FileIcon.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Runtime.InteropServices;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Windows.Media.Imaging;
  11. namespace GeekDesk.Util
  12. {
  13. class FileIcon
  14. {
  15. public static Icon GetIcon(string filePath)
  16. {
  17. IntPtr hIcon = GetJumboIcon(GetIconIndex(filePath));
  18. Icon ico = Icon.FromHandle(hIcon);
  19. return ico;
  20. }
  21. public static BitmapImage GetBitmapImage(string filePath)
  22. {
  23. //Icon ico;
  24. //BitmapImage bmpImage = null;
  25. //MemoryStream strm;
  26. //using (ico = GetIcon(filePath))
  27. //{
  28. // Bitmap bmp = ico.ToBitmap();
  29. // using (strm = new MemoryStream())
  30. // {
  31. // bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
  32. // bmpImage = new BitmapImage();
  33. // bmpImage.BeginInit();
  34. // strm.Seek(0, SeekOrigin.Begin);
  35. // bmpImage.StreamSource = strm;
  36. // bmpImage.EndInit();
  37. // }
  38. //}
  39. //return bmpImage;
  40. Icon ico = GetIcon(filePath);
  41. Bitmap bmp = ico.ToBitmap();
  42. MemoryStream strm = new MemoryStream();
  43. bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
  44. BitmapImage bmpImage = new BitmapImage();
  45. bmpImage.BeginInit();
  46. strm.Seek(0, SeekOrigin.Begin);
  47. bmpImage.StreamSource = strm;
  48. bmpImage.EndInit();
  49. return bmpImage.Clone();
  50. }
  51. public static int GetIconIndex(string pszFile)
  52. {
  53. SHFILEINFO sfi = new SHFILEINFO();
  54. Shell32.SHGetFileInfo(pszFile
  55. , 0
  56. , ref sfi
  57. , (uint)System.Runtime.InteropServices.Marshal.SizeOf(sfi)
  58. , (uint)(SHGFI.SysIconIndex | SHGFI.LargeIcon | SHGFI.UseFileAttributes));
  59. return sfi.iIcon;
  60. }
  61. // 256*256
  62. public static IntPtr GetJumboIcon(int iImage)
  63. {
  64. IImageList spiml = null;
  65. Guid guil = new Guid(IID_IImageList2);//or IID_IImageList
  66. Shell32.SHGetImageList(Shell32.SHIL_JUMBO, ref guil, ref spiml);
  67. IntPtr hIcon = IntPtr.Zero;
  68. spiml.GetIcon(iImage, Shell32.ILD_TRANSPARENT | Shell32.ILD_IMAGE, ref hIcon);
  69. return hIcon;
  70. }
  71. const string IID_IImageList = "46EB5926-582E-4017-9FDF-E8998DAA0950";
  72. const string IID_IImageList2 = "192B9D83-50FC-457B-90A0-2B82A8B5DAE1";
  73. public static class Shell32
  74. {
  75. public const int SHIL_LARGE = 0x0;
  76. public const int SHIL_SMALL = 0x1;
  77. public const int SHIL_EXTRALARGE = 0x2;
  78. public const int SHIL_SYSSMALL = 0x3;
  79. public const int SHIL_JUMBO = 0x4;
  80. public const int SHIL_LAST = 0x4;
  81. public const int ILD_TRANSPARENT = 0x00000001;
  82. public const int ILD_IMAGE = 0x00000020;
  83. [DllImport("shell32.dll", EntryPoint = "#727")]
  84. public extern static int SHGetImageList(int iImageList, ref Guid riid, ref IImageList ppv);
  85. [DllImport("user32.dll", EntryPoint = "DestroyIcon", SetLastError = true)]
  86. public static unsafe extern int DestroyIcon(IntPtr hIcon);
  87. [DllImport("shell32.dll")]
  88. public static extern uint SHGetIDListFromObject([MarshalAs(UnmanagedType.IUnknown)] object iUnknown, out IntPtr ppidl);
  89. [DllImport("Shell32.dll")]
  90. public static extern IntPtr SHGetFileInfo(
  91. string pszPath,
  92. uint dwFileAttributes,
  93. ref SHFILEINFO psfi,
  94. uint cbFileInfo,
  95. uint uFlags
  96. );
  97. }
  98. [Flags]
  99. enum SHGFI : uint
  100. {
  101. /// <summary>get icon</summary>
  102. Icon = 0x000000100,
  103. /// <summary>get display name</summary>
  104. DisplayName = 0x000000200,
  105. /// <summary>get type name</summary>
  106. TypeName = 0x000000400,
  107. /// <summary>get attributes</summary>
  108. Attributes = 0x000000800,
  109. /// <summary>get icon location</summary>
  110. IconLocation = 0x000001000,
  111. /// <summary>return exe type</summary>
  112. ExeType = 0x000002000,
  113. /// <summary>get system icon index</summary>
  114. SysIconIndex = 0x000004000,
  115. /// <summary>put a link overlay on icon</summary>
  116. LinkOverlay = 0x000008000,
  117. /// <summary>show icon in selected state</summary>
  118. Selected = 0x000010000,
  119. /// <summary>get only specified attributes</summary>
  120. Attr_Specified = 0x000020000,
  121. /// <summary>get large icon</summary>
  122. LargeIcon = 0x000000000,
  123. /// <summary>get small icon</summary>
  124. SmallIcon = 0x000000001,
  125. /// <summary>get open icon</summary>
  126. OpenIcon = 0x000000002,
  127. /// <summary>get shell size icon</summary>
  128. ShellIconSize = 0x000000004,
  129. /// <summary>pszPath is a pidl</summary>
  130. PIDL = 0x000000008,
  131. /// <summary>use passed dwFileAttribute</summary>
  132. UseFileAttributes = 0x000000010,
  133. /// <summary>apply the appropriate overlays</summary>
  134. AddOverlays = 0x000000020,
  135. /// <summary>Get the index of the overlay in the upper 8 bits of the iIcon</summary>
  136. OverlayIndex = 0x000000040,
  137. }
  138. [StructLayout(LayoutKind.Sequential)]
  139. public struct SHFILEINFO
  140. {
  141. public const int NAMESIZE = 80;
  142. public IntPtr hIcon;
  143. public int iIcon;
  144. public uint dwAttributes;
  145. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  146. public string szDisplayName;
  147. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
  148. public string szTypeName;
  149. };
  150. [StructLayout(LayoutKind.Sequential)]
  151. public struct RECT
  152. {
  153. public int left, top, right, bottom;
  154. }
  155. [StructLayout(LayoutKind.Sequential)]
  156. public struct POINT
  157. {
  158. int x;
  159. int y;
  160. }
  161. [StructLayout(LayoutKind.Sequential)]
  162. public struct IMAGELISTDRAWPARAMS
  163. {
  164. public int cbSize;
  165. public IntPtr himl;
  166. public int i;
  167. public IntPtr hdcDst;
  168. public int x;
  169. public int y;
  170. public int cx;
  171. public int cy;
  172. public int xBitmap; // x offest from the upperleft of bitmap
  173. public int yBitmap; // y offset from the upperleft of bitmap
  174. public int rgbBk;
  175. public int rgbFg;
  176. public int fStyle;
  177. public int dwRop;
  178. public int fState;
  179. public int Frame;
  180. public int crEffect;
  181. }
  182. [StructLayout(LayoutKind.Sequential)]
  183. public struct IMAGEINFO
  184. {
  185. public IntPtr hbmImage;
  186. public IntPtr hbmMask;
  187. public int Unused1;
  188. public int Unused2;
  189. public RECT rcImage;
  190. }
  191. [ComImportAttribute()]
  192. [GuidAttribute("46EB5926-582E-4017-9FDF-E8998DAA0950")]
  193. [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
  194. public interface IImageList
  195. {
  196. [PreserveSig]
  197. int Add(
  198. IntPtr hbmImage,
  199. IntPtr hbmMask,
  200. ref int pi);
  201. [PreserveSig]
  202. int ReplaceIcon(
  203. int i,
  204. IntPtr hicon,
  205. ref int pi);
  206. [PreserveSig]
  207. int SetOverlayImage(
  208. int iImage,
  209. int iOverlay);
  210. [PreserveSig]
  211. int Replace(
  212. int i,
  213. IntPtr hbmImage,
  214. IntPtr hbmMask);
  215. [PreserveSig]
  216. int AddMasked(
  217. IntPtr hbmImage,
  218. int crMask,
  219. ref int pi);
  220. [PreserveSig]
  221. int Draw(
  222. ref IMAGELISTDRAWPARAMS pimldp);
  223. [PreserveSig]
  224. int Remove(int i);
  225. [PreserveSig]
  226. int GetIcon(
  227. int i,
  228. int flags,
  229. ref IntPtr picon);
  230. [PreserveSig]
  231. int GetImageInfo(
  232. int i,
  233. ref IMAGEINFO pImageInfo);
  234. [PreserveSig]
  235. int Copy(
  236. int iDst,
  237. IImageList punkSrc,
  238. int iSrc,
  239. int uFlags);
  240. [PreserveSig]
  241. int Merge(
  242. int i1,
  243. IImageList punk2,
  244. int i2,
  245. int dx,
  246. int dy,
  247. ref Guid riid,
  248. ref IntPtr ppv);
  249. [PreserveSig]
  250. int Clone(
  251. ref Guid riid,
  252. ref IntPtr ppv);
  253. [PreserveSig]
  254. int GetImageRect(
  255. int i,
  256. ref RECT prc);
  257. [PreserveSig]
  258. int GetIconSize(
  259. ref int cx,
  260. ref int cy);
  261. [PreserveSig]
  262. int SetIconSize(
  263. int cx,
  264. int cy);
  265. [PreserveSig]
  266. int GetImageCount(ref int pi);
  267. [PreserveSig]
  268. int SetImageCount(
  269. int uNewCount);
  270. [PreserveSig]
  271. int SetBkColor(
  272. int clrBk,
  273. ref int pclr);
  274. [PreserveSig]
  275. int GetBkColor(
  276. ref int pclr);
  277. [PreserveSig]
  278. int BeginDrag(
  279. int iTrack,
  280. int dxHotspot,
  281. int dyHotspot);
  282. [PreserveSig]
  283. int EndDrag();
  284. [PreserveSig]
  285. int DragEnter(
  286. IntPtr hwndLock,
  287. int x,
  288. int y);
  289. [PreserveSig]
  290. int DragLeave(
  291. IntPtr hwndLock);
  292. [PreserveSig]
  293. int DragMove(
  294. int x,
  295. int y);
  296. [PreserveSig]
  297. int SetDragCursorImage(
  298. ref IImageList punk,
  299. int iDrag,
  300. int dxHotspot,
  301. int dyHotspot);
  302. [PreserveSig]
  303. int DragShowNolock(
  304. int fShow);
  305. [PreserveSig]
  306. int GetDragImage(
  307. ref POINT ppt,
  308. ref POINT pptHotspot,
  309. ref Guid riid,
  310. ref IntPtr ppv);
  311. [PreserveSig]
  312. int GetItemFlags(
  313. int i,
  314. ref int dwFlags);
  315. [PreserveSig]
  316. int GetOverlayImage(
  317. int iOverlay,
  318. ref int piIndex);
  319. };
  320. }
  321. }