Forum guidelines
Extract icon from exe, dll, or ico file
Posted By justin_yh on 07 Mar 2004
I make an application that is similar to windows explorer. The problem is I don't know how to extract the icon from a file. I learn from a post about the same problem in vb. This is the code that I translate from vb to c#: [DllImport("shell32.dll")] public static extern long ExtractIcon(long lngInst, string strExeFileName, long lngIconIndex); [DllImport("user32.dll")] public static extern long DrawIcon(long lngDC, long lngX, long lngY, long lngIcon); the problem for this codes is, What are the lngInst and lngDC in C#? Please help me to do this. Thanks in advanced.
Re: Extract icon from exe, dll, or ico file
Posted By cprie on 08 Mar 2004  
I had the same problem when trying to display the correct icons for word docs, htmls .... , in my app. the functions i've used for that are ExtractIconEx to load the icons from the files and DestroyIcon to release them. ------------------------ /// /// Function to extract Icons from Dll or Exe file. /// [DllImport("Shell32.dll")] public static extern uint ExtractIconEx(string szFile, // file name int iconIndex, // icon index System.IntPtr[] iconLarge, // large icon array System.IntPtr[] iconSmall, // small icon array uint icons ); // number of icons to extract /// /// Function to Release Resources allocated by ExtractIconEx Function. /// [DllImport("user32.dll")] public static extern uint DestroyIcon(System.IntPtr hIcon); ----------------------------- cause i haven't found out how to load the icons directly from the doc (i dont think it's possible) i search the registry for the file extension and the registered icon Namespace for registry : using Microsoft.Win32; ---- heres my function /// /// Search Registry for extension and extract the icon for the extension. /// /// extension for which the bitmap should be found /// (.htm, .doc, ...) /// if true a icon with size 32x32 is extracted from /// registry; else a icon with size 16x16 is extracted /// bitmap which is registered for this extension or NULL if no one was /// found or a error occured public static System.Drawing.Image FindImage(string filePath, bool largeImage) { string fileName = System.IO.Path.GetFileName(filePath).ToLower(); // files extension - used to search the registry string extension = System.IO.Path.GetExtension(filePath).ToLower(); // bitmap which is returned System.Drawing.Bitmap myBitmap = null; RegistryKey regKey = Registry.ClassesRoot; RegistryKey iconKey = null; try { // if file is an icon file load the icon directly from the file if(extension == ".ico") myBitmap = largeImage ? new System.Drawing.Bitmap(new Bitmap(filePath), 32, 32) : new System.Drawing.Bitmap(new Bitmap(filePath), 16, 16); // IntPtr are used in the ExtractIconEx function System.IntPtr[] small = new System.IntPtr[ 1 ]; System.IntPtr[] large = new System.IntPtr[ 1 ]; // open extension subKey regKey = regKey.OpenSubKey( extension ); // get default entry string newKeyName = regKey != null ? regKey.GetValue("").ToString() : null; if(newKeyName == null) return null; // set registryKey to ClassesRoot and opens the previous found registry key regKey = Registry.ClassesRoot; regKey = regKey.OpenSubKey( newKeyName ); // get the default icon registry key iconKey = regKey.OpenSubKey("DefaultIcon", false); if(iconKey == null) { // if no default icon reg key was found continue search by searching for the open or edit commands // the extension is registered with iconKey = regKey.OpenSubKey("shell", false); // if a open subkey exist use it otherwise search for an edit key if(new ArrayList(iconKey.GetSubKeyNames()).Contains("open")) iconKey = iconKey.OpenSubKey("open", false); else if(new ArrayList(iconKey.GetSubKeyNames()).Contains("edit")) iconKey = iconKey.OpenSubKey("edit", false); // if no open or edit key exists set value to null and a default icon 'll be used else iconKey = null; // return a default bitmap if no one was found if(iconKey == null) { if(largeImage) myBitmap = new Bitmap(new Bitmap(""), 32, 32); else myBitmap = new Bitmap(new Bitmap(""), 16, 16); return myBitmap; } // otherwise open the command subkey to get the programs exe file from where the icon can be extracted iconKey = iconKey.OpenSubKey("command", false); if(iconKey != null) regKey = iconKey; } else regKey = iconKey; // open defaultIcon subkex and get the icon index string iconPath = regKey.GetValue("").ToString(); // split the found command into command path (exe) and icon index string[] iconPath1 = iconPath.Split(','); string commandPath = iconPath1[0].Substring(0, iconPath1[0].LastIndexOf(".")+4).Trim('\"'); int iconIndex = iconPath1.Length > 1 ? Convert.ToInt32(iconPath1[1]) : 0; // extract the icon from the found exe or dll at found index uint returnvalue = ExtractIconEx(commandPath, iconIndex, large, small, 1); // if no icon was found - set return value to default Icon if ( returnvalue == 0 || returnvalue == uint.MaxValue) { if(largeImage) myBitmap = new Bitmap(new Bitmap(""), 32, 32); else myBitmap = new Bitmap(new Bitmap(""), 16, 16); } // if largeIcon = true , set return value to large Icon if ( largeImage && large[ 0 ] != IntPtr.Zero ) myBitmap = Bitmap.FromHicon(large[0]); // else set return value to small icon else if ( !largeImage && small[ 0 ] != IntPtr.Zero ) { // Icon from registry myBitmap = Bitmap.FromHicon(small[0]); } // destroy icons and free the resources allocated cause this wont be done by the framework if(large[0] != IntPtr.Zero) returnvalue = DestroyIcon(large[0]); if(small[0] != IntPtr.Zero) returnvalue = DestroyIcon(small[0]); } catch(Exception ex) { ex.ToString(); return null; } finally { // close all open registry keys if(regKey != null) regKey.Close(); if(iconKey != null) iconKey.Close(); } // return found Icon return myBitmap; } I have tested it under windows 2000 and winxp. I'm not sure about win 98 cause the registry isn't the same under the diffrent os versions. I hate the registry -> if your code works under win2k it wont do the same under winxp!! Hope this 'll solve your problem or gives you a hint to do it in a better way than i. For questions on that code send me a mail to prie
Aspose File APIs


Spire.Doc - Free .NET Word API
Use Spire.Doc to create, read, write, print, and convert word documents to OpenXML, RTF, TXT, XPS, EPUB, EMF, HTML, Image and PDF.