Image GetIcon(string file) { Icon ficon; WinAPI.SHFILEINFO shinfo = new WinAPI.SHFILEINFO(); IntPtr ptr = WinAPI.SHGetFileInfo(file, WinAPI.FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), WinAPI.SHGFI_SYSICONINDEX); if (ptr == IntPtr.Zero) { ficon = Icon.ExtractAssociatedIcon(file); } else { int iconIndex = shinfo.iIcon; Guid iImageListGuid = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); WinAPI.IImageList iml; int hres = WinAPI.SHGetImageList(0x04, ref iImageListGuid, out iml); IntPtr hIcon = IntPtr.Zero; hres = iml.GetIcon(iconIndex, 1, ref hIcon); ficon = System.Drawing.Icon.FromHandle(hIcon); } Image img; Icon temp = new Icon(ficon, 128, 128); img = temp.ToBitmap(); temp.Dispose(); ficon.Dispose(); return(img); }
Image GetIcon(string file) { Icon ficon; WinAPI.SHFILEINFO shinfo = new WinAPI.SHFILEINFO(); IntPtr ptr = WinAPI.SHGetFileInfo(file, WinAPI.FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), WinAPI.SHGFI_SYSICONINDEX); if (ptr == IntPtr.Zero) ficon = Icon.ExtractAssociatedIcon(file); else { int iconIndex = shinfo.iIcon; Guid iImageListGuid = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); WinAPI.IImageList iml; int hres = WinAPI.SHGetImageList(0x04, ref iImageListGuid, out iml); IntPtr hIcon = IntPtr.Zero; hres = iml.GetIcon(iconIndex, 1, ref hIcon); ficon = System.Drawing.Icon.FromHandle(hIcon); } Image img; Icon temp = new Icon(ficon, 128, 128); img = temp.ToBitmap(); temp.Dispose(); ficon.Dispose(); return img; }