public static bool getIcon(string file, out Image image) { image = null; SHFILEINFO shFileInfo = new SHFILEINFO(); NativeMethods.SHGetFileInfo(file, 0, out shFileInfo, (uint)Marshal.SizeOf(shFileInfo), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON | 0x4); if (shFileInfo.hIcon != IntPtr.Zero) { image = Icon.FromHandle(shFileInfo.hIcon).ToBitmap(); NativeMethods.DestroyIcon(shFileInfo.hIcon); return true; } return false; }
private IconCache() { ImageAddIcon(@"c:\", list); SHFILEINFO shFileInfo = new SHFILEINFO(); NativeMethods.SHGetFileInfo("", 0, out shFileInfo, (uint)Marshal.SizeOf(shFileInfo), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON | 0x4); if (shFileInfo.hIcon != IntPtr.Zero) { diricon = Icon.FromHandle(shFileInfo.hIcon).ToBitmap(); NativeMethods.DestroyIcon(shFileInfo.hIcon); } cache.ImageSize = new Size(16, 16); cache.ColorDepth = ColorDepth.Depth32Bit; //list.ColorDepth = ColorDepth.Depth32Bit; //ImageAddIcon("shell32.dll", list); }
public Image getImage(string path, string ext, bool isfile) { if (!isfile) { return diricon; } //if(ext==".lnk"){ // SHFILEINFO shFileInfo = new SHFILEINFO(); // NativeMethods.SHGetFileInfo(path, 0, out shFileInfo, // (uint)Marshal.SizeOf(shFileInfo), NativeMethods.SHGFI_ICON | // NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_OVERLAYINDEX); // var iconIndex = (shFileInfo.iIcon & 0xFFFFFF); // if (shFileInfo.hIcon != IntPtr.Zero) { // NativeMethods.DestroyIcon(shFileInfo.hIcon); // } // Image res; // if (ImageAddIcon(@"%SystemRoot%\System32\shell32.dll", iconIndex, out res)) { // return res; // } // return list.Images[iconIndex]; //} //var i = cache.Images.ContainsKey if (!cache.Images.ContainsKey(ext)) { SHFILEINFO shFileInfo = new SHFILEINFO(); NativeMethods.SHGetFileInfo(path, 0, out shFileInfo, (uint)Marshal.SizeOf(shFileInfo), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON | 0x4); if (shFileInfo.hIcon != IntPtr.Zero) { cache.Images.Add(ext, Icon.FromHandle(shFileInfo.hIcon).ToBitmap()); NativeMethods.DestroyIcon(shFileInfo.hIcon); } } return cache.Images[ext]; }
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint uFlags);