IconExtractor.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * IconExtractor/IconUtil for .NET
  3. * Copyright (C) 2014 Tsuda Kageyu. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  17. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  18. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
  19. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.ComponentModel;
  30. using System.Drawing;
  31. using System.IO;
  32. using System.Runtime.InteropServices;
  33. using System.Windows.Forms;
  34. namespace TurnoffMonitorOnLock.IconExtract
  35. {
  36. internal class IconExtractor
  37. {
  38. ////////////////////////////////////////////////////////////////////////
  39. // Constants
  40. // Flags for LoadLibraryEx().
  41. private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002;
  42. // Resource types for EnumResourceNames().
  43. private static readonly IntPtr RtIcon = (IntPtr)3;
  44. private static readonly IntPtr RtGroupIcon = (IntPtr)14;
  45. ////////////////////////////////////////////////////////////////////////
  46. // Fields
  47. private byte[][] _iconData = null; // Binary data of each icon.
  48. ////////////////////////////////////////////////////////////////////////
  49. // Public properties
  50. /// <summary>
  51. /// Gets the count of the icons in the associated file.
  52. /// </summary>
  53. public int Count => _iconData.Length;
  54. /// <summary>
  55. /// Initializes a new instance of the IconExtractor class from the specified file name.
  56. /// </summary>
  57. /// <param name="fileName">The file to extract icons from.</param>
  58. public IconExtractor(string fileName) => Initialize(fileName);
  59. /// <summary>
  60. /// Extracts an icon from the file.
  61. /// </summary>
  62. /// <param name="index">Zero based index of the icon to be extracted.</param>
  63. /// <returns>A System.Drawing.Icon object.</returns>
  64. /// <remarks>Always returns new copy of the Icon. It should be disposed by the user.</remarks>
  65. public Icon GetIcon(int index)
  66. {
  67. if (index < 0 || Count <= index) throw new ArgumentOutOfRangeException(nameof(index));
  68. // Create an Icon based on a .ico file in memory.
  69. using (var ms = new MemoryStream(_iconData[index])) return new Icon(ms);
  70. }
  71. private void Initialize(string fileName)
  72. {
  73. if (fileName == null) throw new ArgumentNullException(nameof(fileName));
  74. var hModule = IntPtr.Zero;
  75. try
  76. {
  77. hModule = NativeMethods.LoadLibraryEx(fileName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
  78. if (hModule == IntPtr.Zero) throw new Win32Exception();
  79. // Enumerate the icon resource and build .ico files in memory.
  80. var tmpData = new List<byte[]>();
  81. bool Callback(IntPtr h, IntPtr t, IntPtr name, IntPtr l)
  82. {
  83. // Refer the following URL for the data structures used here:
  84. // http://msdn.microsoft.com/en-us/library/ms997538.aspx
  85. // RT_GROUP_ICON resource consists of a GRPICONDIR and GRPICONDIRENTRY's.
  86. var dir = GetDataFromResource(hModule, RtGroupIcon, name);
  87. // Calculate the size of an entire .icon file.
  88. int count = BitConverter.ToUInt16(dir, 4); // GRPICONDIR.idCount
  89. var len = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
  90. for (var i = 0; i < count; ++i)
  91. len += BitConverter.ToInt32(dir, 6 + 14 * i + 8); // GRPICONDIRENTRY.dwBytesInRes
  92. using (var dst = new BinaryWriter(new MemoryStream(len)))
  93. {
  94. // Copy GRPICONDIR to ICONDIR.
  95. dst.Write(dir, 0, 6);
  96. var picOffset = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
  97. for (var i = 0; i < count; ++i)
  98. {
  99. // Load the picture.
  100. var id = BitConverter.ToUInt16(dir, 6 + 14 * i + 12); // GRPICONDIRENTRY.nID
  101. var pic = GetDataFromResource(hModule, RtIcon, (IntPtr)id);
  102. // Copy GRPICONDIRENTRY to ICONDIRENTRY.
  103. dst.Seek(6 + 16 * i, SeekOrigin.Begin);
  104. dst.Write(dir, 6 + 14 * i, 8); // First 8bytes are identical.
  105. dst.Write(pic.Length); // ICONDIRENTRY.dwBytesInRes
  106. dst.Write(picOffset); // ICONDIRENTRY.dwImageOffset
  107. // Copy a picture.
  108. dst.Seek(picOffset, SeekOrigin.Begin);
  109. dst.Write(pic, 0, pic.Length);
  110. picOffset += pic.Length;
  111. }
  112. tmpData.Add(((MemoryStream)dst.BaseStream).ToArray());
  113. }
  114. return true;
  115. }
  116. NativeMethods.EnumResourceNames(hModule, RtGroupIcon, Callback, IntPtr.Zero);
  117. _iconData = tmpData.ToArray();
  118. }
  119. finally
  120. {
  121. if (hModule != IntPtr.Zero) NativeMethods.FreeLibrary(hModule);
  122. }
  123. }
  124. private static byte[] GetDataFromResource(IntPtr hModule, IntPtr type, IntPtr name)
  125. {
  126. // Load the binary data from the specified resource.
  127. var hResInfo = NativeMethods.FindResource(hModule, name, type);
  128. if (hResInfo == IntPtr.Zero)
  129. throw new Win32Exception();
  130. var hResData = NativeMethods.LoadResource(hModule, hResInfo);
  131. if (hResData == IntPtr.Zero)
  132. throw new Win32Exception();
  133. var pResData = NativeMethods.LockResource(hResData);
  134. if (pResData == IntPtr.Zero)
  135. throw new Win32Exception();
  136. var size = NativeMethods.SizeofResource(hModule, hResInfo);
  137. if (size == 0)
  138. throw new Win32Exception();
  139. var buf = new byte[size];
  140. Marshal.Copy(pResData, buf, 0, buf.Length);
  141. return buf;
  142. }
  143. public static Icon GetMainIcon()
  144. {
  145. var iex = new IconExtractor(Application.ExecutablePath);
  146. return iex.Count > 0 ? iex.GetIcon(0) : null;
  147. }
  148. }
  149. }