FileIcon.cs 9.9 KB

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