■ Icon 클래스를 사용해 파일에서 아이콘 파일을 만드는 방법을 보여준다.
▶ IconFactory.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 아이콘 헬퍼 /// </summary> public class IconHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region SH 변경 통지하기 - SHChangeNotify(eventID, flag, item1Handle, item2Handle) /// <summary> /// SH 변경 통지하기 /// </summary> /// <param name="eventID">이벤트 ID</param> /// <param name="flag">플래그</param> /// <param name="item1Handle">항목 1 핸들</param> /// <param name="item2Handle">항목 2 핸들</param> [DllImport("shell32.dll")] private static extern void SHChangeNotify ( SHChangeNotifyEventID eventID, SHChangeNotifyFlag flag, IntPtr item1Handle, IntPtr item2Handle ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration ////////////////////////////////////////////////////////////////////////////////////////// Private #region SH 변경 통지 이벤트 ID - SHChangeNotifyEventID /// <summary> /// SH 변경 통지 이벤트 ID /// </summary> [Flags] private enum SHChangeNotifyEventID { /// <summary> /// SHCNE_ALLEVENTS /// </summary> SHCNE_ALLEVENTS = 0x7FFFFFFF, /// <summary> /// SHCNE_ASSOCCHANGED /// </summary> SHCNE_ASSOCCHANGED = 0x08000000, /// <summary> /// SHCNE_ATTRIBUTES /// </summary> SHCNE_ATTRIBUTES = 0x00000800, /// <summary> /// SHCNE_CREATE /// </summary> SHCNE_CREATE = 0x00000002, /// <summary> /// SHCNE_DELETE /// </summary> SHCNE_DELETE = 0x00000004, /// <summary> /// SHCNE_DRIVEADD /// </summary> SHCNE_DRIVEADD = 0x00000100, /// <summary> /// SHCNE_DRIVEADDGUI /// </summary> SHCNE_DRIVEADDGUI = 0x00010000, /// <summary> /// SHCNE_DRIVEREMOVED /// </summary> SHCNE_DRIVEREMOVED = 0x00000080, /// <summary> /// SHCNE_EXTENDED_EVENT /// </summary> SHCNE_EXTENDED_EVENT = 0x04000000, /// <summary> /// SHCNE_FREESPACE /// </summary> SHCNE_FREESPACE = 0x00040000, /// <summary> /// SHCNE_MEDIAINSERTED /// </summary> SHCNE_MEDIAINSERTED = 0x00000020, /// <summary> /// SHCNE_MEDIAREMOVED /// </summary> SHCNE_MEDIAREMOVED = 0x00000040, /// <summary> /// SHCNE_MKDIR /// </summary> SHCNE_MKDIR = 0x00000008, /// <summary> /// SHCNE_NETSHARE /// </summary> SHCNE_NETSHARE = 0x00000200, /// <summary> /// SHCNE_NETUNSHARE /// </summary> SHCNE_NETUNSHARE = 0x00000400, /// <summary> /// SHCNE_RENAMEFOLDER /// </summary> SHCNE_RENAMEFOLDER = 0x00020000, /// <summary> /// SHCNE_RENAMEITEM /// </summary> SHCNE_RENAMEITEM = 0x00000001, /// <summary> /// SHCNE_RMDIR /// </summary> SHCNE_RMDIR = 0x00000010, /// <summary> /// SHCNE_SERVERDISCONNECT /// </summary> SHCNE_SERVERDISCONNECT = 0x00004000, /// <summary> /// SHCNE_UPDATEDIR /// </summary> SHCNE_UPDATEDIR = 0x00001000, /// <summary> /// SHCNE_UPDATEIMAGE /// </summary> SHCNE_UPDATEIMAGE = 0x00008000, } #endregion #region SH 변경 통지 플래그 - SHChangeNotifyFlag /// <summary> /// SH 변경 통지 플래그 /// </summary> private enum SHChangeNotifyFlag : uint { /// <summary> /// SHCNF_IDLIST /// </summary> SHCNF_IDLIST = 0x0000, /// <summary> /// SHCNF_PATHA /// </summary> SHCNF_PATHA = 0x0001, /// <summary> /// SHCNF_PRINTERA /// </summary> SHCNF_PRINTERA = 0x0002, /// <summary> /// SHCNF_DWORD /// </summary> SHCNF_DWORD = 0x0003, /// <summary> /// SHCNF_PATHW /// </summary> SHCNF_PATHW = 0x0005, /// <summary> /// SHCNF_PRINTERW /// </summary> SHCNF_PRINTERW = 0x0006, /// <summary> /// SHCNF_TYPE /// </summary> SHCNF_TYPE = 0x00FF, /// <summary> /// SHCNF_FLUSH /// </summary> SHCNF_FLUSH = 0x1000, /// <summary> /// SHCNF_FLUSHNOWAIT /// </summary> SHCNF_FLUSHNOWAIT = 0x2000, /// <summary> /// SHCNF_PATH /// </summary> SHCNF_PATH = SHCNF_PATHW, /// <summary> /// SHCNF_PRINTER /// </summary> SHCNF_PRINTER = SHCNF_PRINTERW } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 최대 아이콘 너비 /// </summary> public const int MAXIMUM_ICON_WIDTH = 256; /// <summary> /// 최대 아이콘 높이 /// </summary> public const int MAXIMUM_ICON_HEIGHT = 256; /// <summary> /// 헤더 예약 /// </summary> private const ushort HEADER_RESERVED = 0; /// <summary> /// 헤더 아이콘 타입 /// </summary> private const ushort HEADER_ICON_TYPE = 1; /// <summary> /// 헤더 길이 /// </summary> private const byte HEADER_LENGTH = 6; /// <summary> /// 엔트리 예약 /// </summary> private const byte ENTRY_RESERVED = 0; /// <summary> /// 엔트리 길이 /// </summary> private const byte ENTRY_LENGTH = 16; /// <summary> /// 팔레트 내 PNG 색상 수 /// </summary> private const byte PNG_COLOR_COUNT_IN_PALETTE = 0; /// <summary> /// PNG 색상 플레인 수 /// </summary> private const ushort PNG_COLOR_PLANE_COUNT = 1; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 저장하기 - Save(bitmapEnumerable, stream, notifySystem) /// <summary> /// 저장하기 /// </summary> /// <param name="bitmapEnumerable">비트맵 열거 가능형</param> /// <param name="stream">스트림</param> /// <param name="notifySystem">시스템 통지 여부</param> public static void Save(IEnumerable<Bitmap> bitmapEnumerable, Stream stream, bool notifySystem) { Bitmap[] bitmapArray = bitmapEnumerable.OrderBy(i => i.Width).ThenBy(i => i.Height).ToArray(); using(BinaryWriter writer = new BinaryWriter(stream)) { writer.Write(HEADER_RESERVED); writer.Write(HEADER_ICON_TYPE); writer.Write((ushort)bitmapArray.Length); Dictionary<uint, byte[]> dictionary = new Dictionary<uint, byte[]>(); uint lengthSummary = 0; uint baseOffset = (uint)(HEADER_LENGTH * ENTRY_LENGTH * bitmapArray.Length); for(int i = 0; i < bitmapArray.Length; i++) { Bitmap bitmap = bitmapArray[i]; byte[] byteArray = GetBitmapByteArray(bitmap); uint offset = baseOffset + lengthSummary; writer.Write(GetIconWidth(bitmap)); writer.Write(GetIconHeight(bitmap)); writer.Write(PNG_COLOR_COUNT_IN_PALETTE); writer.Write(ENTRY_RESERVED); writer.Write(PNG_COLOR_PLANE_COUNT); writer.Write((ushort)Image.GetPixelFormatSize(bitmap.PixelFormat)); writer.Write((uint)byteArray.Length); writer.Write(offset); lengthSummary += (uint)byteArray.Length; dictionary.Add(offset, byteArray); } foreach(KeyValuePair<uint, byte[]> keyValuePair in dictionary) { writer.BaseStream.Seek(keyValuePair.Key, SeekOrigin.Begin); writer.Write(keyValuePair.Value); } } if(notifySystem) { SHChangeNotify ( SHChangeNotifyEventID.SHCNE_ASSOCCHANGED, SHChangeNotifyFlag.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero ); } } #endregion #region 저장하기 - Save(bitmapEnumerable) /// <summary> /// 저장하기 /// </summary> /// <param name="bitmapEnumerable">비트맵 열거 가능형</param> /// <returns>아이콘</returns> public static Icon Save(IEnumerable<Bitmap> bitmapEnumerable) { using(MemoryStream stream1 = new MemoryStream()) { Save(bitmapEnumerable, stream1, false); using(MemoryStream stream2 = new MemoryStream(stream1.ToArray())) { return new Icon(stream2); } } } #endregion #region 저장하기- Save(bitmapEnumerable, filePath, notifySystem) /// <summary> /// 저장하기 /// </summary> /// <param name="bitmapEnumerable">비트맵 열거 가능형</param> /// <param name="filePath">파일 경로</param> /// <param name="notifySystem">시스템 통지 여부</param> public static void Save(IEnumerable<Bitmap> bitmapEnumerable, string filePath, bool notifySystem) { using(FileStream stream = new FileStream(filePath, FileMode.Create)) { Save(bitmapEnumerable, stream, notifySystem); } } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 비트맵 바이트 배열 구하기 - GetBitmapByteArray(bitmap) /// <summary> /// 비트맵 바이트 배열 구하기 /// </summary> /// <param name="bitmap">비트맵</param> /// <returns>비트맵 바이트 배열</returns> private static byte[] GetBitmapByteArray(Bitmap bitmap) { using(MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); return stream.ToArray(); } } #endregion #region 아이콘 너비 구하기 - GetIconWidth(bitmap) /// <summary> /// 아이콘 너비 구하기 /// </summary> /// <param name="bitmap">비트맵</param> /// <returns>아이콘 너비</returns> private static byte GetIconWidth(Bitmap bitmap) { if(bitmap.Width >= MAXIMUM_ICON_WIDTH) { return 0; } return (byte)bitmap.Width; } #endregion #region 아이콘 높이 구하기 - GetIconHeight(bitmap) /// <summary> /// 아이콘 높이 구하기 /// </summary> /// <param name="bitmap">비트맵</param> /// <returns>아이콘 높이</returns> private static byte GetIconHeight(Bitmap bitmap) { if(bitmap.Height >= MAXIMUM_ICON_HEIGHT) { return 0; } return (byte)bitmap.Height; } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); Load += Form_Load; this.openMenuItem.Click += openMenuItem_Click; this.saveAsMenuItem.Click += saveAsMenuItem_Click; this.exitMenuItem.Click += exitMenuItem_Click; this.clearMenuItem.Click += clearMenuItem_Click; this.listView.KeyDown += listView_KeyDown; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { this.imageList.ImageSize = new Size(64, 64); this.listView.View = View.Tile; this.listView.LargeImageList = this.imageList; } #endregion #region 열기 메뉴 항목 클릭시 처리하기 - openMenuItem_Click(sender, e) /// <summary> /// 열기 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void openMenuItem_Click(object sender, EventArgs e) { if(this.openFileDialog.ShowDialog() == DialogResult.OK) { foreach(string filePath in openFileDialog.FileNames) { Bitmap bitmap = new Bitmap(filePath); this.imageList.Images.Add(bitmap); this.listView.Items.Add(filePath, this.imageList.Images.Count - 1); } this.saveAsMenuItem.Enabled = (this.listView.Items.Count > 0); } } #endregion #region 다른 이름으로 저장 메뉴 항목 클릭시 처리하기 - saveAsMenuItem_Click(sender, e) /// <summary> /// 다른 이름으로 저장 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveAsMenuItem_Click(object sender, EventArgs e) { if(this.saveFileDialog.ShowDialog() == DialogResult.OK) { List<Bitmap> bitmapList = new List<Bitmap>(); foreach(ListViewItem item in this.listView.Items) { bitmapList.Add(new Bitmap(item.Text)); } IconHelper.Save(bitmapList, this.saveFileDialog.FileName, true); Icon = new Icon(this.saveFileDialog.FileName); } } #endregion #region 종료 메뉴 항목 클릭시 처리하기 - exitMenuItem_Click(sender, e) /// <summary> /// 종료 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void exitMenuItem_Click(object sender, EventArgs e) { Close(); } #endregion #region 지우기 메뉴 항목 클릭시 처리하기 - clearMenuItem_Click(sender, e) /// <summary> /// 지우기 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void clearMenuItem_Click(object sender, EventArgs e) { this.listView.Items.Clear(); this.imageList.Images.Clear(); this.saveAsMenuItem.Enabled = (this.listView.Items.Count > 0); } #endregion #region 리스트 뷰 키 DOWN 처리하기 - listView_KeyDown(sender, e) /// <summary> /// 리스트 뷰 키 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void listView_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Delete) { foreach(ListViewItem item in listView.SelectedItems) { item.Remove(); } if(this.listView.Items.Count == 0) { this.saveAsMenuItem.Enabled = false; this.imageList.Images.Clear(); } } } #endregion } } |