private void LoadImage() { Win32.SHSTOCKICONINFO sii = new Win32.SHSTOCKICONINFO(); sii.cbSize = Marshal.SizeOf(sii); Win32.SHGetStockIconInfo(Win32.SIID_FOLDER, Win32.SHGSI_ICON, ref sii); if (sii.hIcon != IntPtr.Zero) { Icon aIcon = Icon.FromHandle(sii.hIcon); imageList_icon.Images.Add("Folder", aIcon); } Win32.IImageList imglist = null; int rsult = Win32.SHGetImageList(Win32.SHIL_EXTRALARGE, ref Win32.IID_IImageList, out imglist); IntPtr hicon = IntPtr.Zero; Win32.SHGetStockIconInfo(Win32.SIID_STUFFEDFOLDER, Win32.SHGSI_ICON, ref sii); if (sii.hIcon != IntPtr.Zero) { Icon aIcon = Icon.FromHandle(sii.hIcon); imageList_icon.Images.Add("Folder2", aIcon); } Win32.SHGetStockIconInfo(Win32.SIID_DOCNOASSOC, Win32.SHGSI_ICON, ref sii); if (sii.hIcon != IntPtr.Zero) { Icon aIcon = Icon.FromHandle(sii.hIcon); imageList_icon.Images.Add("Doc", aIcon); } treeView1.ImageList = imageList_icon; }
public static Icon GetIcon(string fileName, IconSize thisSize) { var shinfo = new Win32.SHFILEINFOW(); Icon myIcon; int flags, res; switch (thisSize) { case IconSize.Small: case IconSize.Large: { if (thisSize == IconSize.Small) { flags = Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON; } else { flags = Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON; } flags = flags | Win32.SHGFI_USEFILEATTRIBUTES; res = Win32.SHGetFileInfoW(fileName, 0, ref shinfo, Marshal.SizeOf(shinfo), (uint)flags); if (res == 0) { throw (new System.IO.FileNotFoundException()); } myIcon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone(); Win32.DestroyIcon(shinfo.hIcon); break; } case IconSize.ExtraLarge: case IconSize.Jumbo: { flags = Win32.SHGFI_SYSICONINDEX; flags = flags | Win32.SHGFI_USEFILEATTRIBUTES; res = Win32.SHGetFileInfoW(fileName, Win32.FILE_ATTRIBUTE_NORMAL, ref shinfo, Marshal.SizeOf(shinfo), (uint)flags); if (res == 0) { throw new System.IO.FileNotFoundException(); } var iconIndex = shinfo.iIcon; var iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); Win32.IImageList iml = null; int size; if (thisSize == IconSize.ExtraLarge) { size = System.Convert.ToInt32(Win32.SHIL_EXTRALARGE); } else { size = System.Convert.ToInt32(Win32.SHIL_JUMBO); } Win32.SHGetImageList(size, ref iidImageList, ref iml); var hIcon = IntPtr.Zero; iml.GetIcon(iconIndex, (int)Win32.ILD_FLAGS.ILD_IMAGE, ref hIcon); myIcon = (Icon)Icon.FromHandle(hIcon).Clone(); Win32.DestroyIcon(hIcon); var bmp = myIcon.ToBitmap(); if (bmp.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb) { myIcon.Dispose(); myIcon = null /* TODO Change to default(_) if this is not a reference type */; iml.GetIcon(iconIndex, (int)Win32.ILD_FLAGS.ILD_TRANSPARENT, ref hIcon); myIcon = (Icon)Icon.FromHandle(hIcon).Clone(); Win32.DestroyIcon(hIcon); } break; } default: { return(null /* TODO Change to default(_) if this is not a reference type */); } } return(myIcon); }
public static extern int SHGetImageList(int iImageList, ref Guid riid, out Win32.IImageList ppv);