private void loadFromLibrary(string libraryFile, string resourceName) { string message = ""; bool flag = false; IntPtr zero = IntPtr.Zero; IntPtr hResInfo = IntPtr.Zero; IntPtr hInstance = IntPtr.Zero; IntPtr lPtr = IntPtr.Zero; try { hInstance = LoadLibraryEx(libraryFile, IntPtr.Zero, 2); if (hInstance != IntPtr.Zero) { hResInfo = FindResource(hInstance, resourceName, (IntPtr)14); if (hResInfo != IntPtr.Zero) { zero = LoadResource(hInstance, hResInfo); if (zero != IntPtr.Zero) { lPtr = LockResource(zero); if (lPtr != IntPtr.Zero) { int num = this.readResourceIconFileHeader(lPtr); MEMICONDIRENTRY[] memicondirentryArray = new MEMICONDIRENTRY[num]; int ofs = 6; for (int i = 0; i < num; i++) { memicondirentryArray[i] = new MEMICONDIRENTRY(lPtr, ofs); ofs += 14; } FreeResource(zero); zero = IntPtr.Zero; IcDvImg[] icons = new IcDvImg[num]; for (int j = 0; j < num; j++) { string lpName = string.Format("#{0:N0}", memicondirentryArray[j].nID); hResInfo = FindResource(hInstance, lpName, (IntPtr)3); if (hResInfo == IntPtr.Zero) { message = string.Format("Could not find the component icon resource with id {0}", memicondirentryArray[j].nID); flag = true; break; } zero = LoadResource(hInstance, hResInfo); if (zero == IntPtr.Zero) { message = string.Format("Could not load the component icon resource with id {0}", memicondirentryArray[j].nID); flag = true; break; } int length = SizeofResource(hInstance, hResInfo); if ((length > 0) && (length == memicondirentryArray[j].dwBytesInRes)) { lPtr = LockResource(zero); byte[] destination = new byte[length]; Marshal.Copy(lPtr, destination, 0, length); icons[j] = new IcDvImg(destination); } else { message = string.Format("Component icon resource with id {0} is corrupt", memicondirentryArray[j].nID); flag = true; } } if (!flag) { this.iconCollection = new IconDeviceImageCollection(icons); } } else { message = "Can't lock resource for reading."; flag = true; } } else { message = "Can't load resource for reading."; flag = true; } } else { message = "Can't find resource."; flag = true; } } else { message = "Can't load library."; flag = true; } } catch (Exception exception) { flag = true; message = exception.Message; } finally { if (zero != IntPtr.Zero) { FreeResource(zero); } if (hInstance != IntPtr.Zero) { FreeLibrary(hInstance); } if (flag) { throw new IconExException(message); } } }
public static MemoryStream GetIconMemoryStreamFromExeFile(string exeFilePath, int iconIndex, ref MemoryStream imageMemoryStream) { IntPtr iconHandle = IntPtr.Zero; IntPtr libraryHandle = IntPtr.Zero; try { //Load executable file library Debug.WriteLine("Loading exe icon: " + exeFilePath); libraryHandle = LoadLibraryEx(exeFilePath, IntPtr.Zero, LoadLibraryFlags.LOAD_LIBRARY_AS_DATAFILE); if (libraryHandle == IntPtr.Zero) { Debug.WriteLine("Failed to load icon from exe: " + exeFilePath); return(null); } //Enumerate all icon groups List <string> iconGroups = new List <string>(); EnumResNameProcDelegate EnumResourceNamesCallback = (IntPtr hModule, ResourceTypes lpType, IntPtr lpEnumFunc, IntPtr lParam) => { try { string intPtrString = IntPtrToString(lpEnumFunc); if (!string.IsNullOrWhiteSpace(intPtrString)) { iconGroups.Add(intPtrString); return(true); } } catch { } return(false); }; EnumResourceNames(libraryHandle, ResourceTypes.GROUP_ICON, EnumResourceNamesCallback, IntPtr.Zero); //Select target icon group string iconGroup = string.Empty; int iconGroupsCount = iconGroups.Count; //Debug.WriteLine("Total icon groups: " + iconGroupsCount); if (iconGroupsCount > 0 && iconGroupsCount >= iconIndex) { iconGroup = iconGroups[iconIndex]; } else { Debug.WriteLine("No exe icon found to load."); return(null); } //Get all icons from group List <MEMICONDIRENTRY> iconDirEntryList = new List <MEMICONDIRENTRY>(); IntPtr iconDirIntPtr = GetResourceDataIntPtrFromString(libraryHandle, iconGroup, ResourceTypes.GROUP_ICON); unsafe { MEMICONDIR * iconDir = (MEMICONDIR *)iconDirIntPtr; MEMICONDIRENTRY *iconDirEntryArray = &iconDir->idEntriesArray; //Debug.WriteLine("Total icons in group: " + iconDir->idCount); for (int entryId = 0; entryId < iconDir->idCount; entryId++) { try { iconDirEntryList.Add(iconDirEntryArray[entryId]); } catch { } } } //Select largest icon MEMICONDIRENTRY iconDirEntry = iconDirEntryList.OrderByDescending(x => x.wBitCount).ThenByDescending(x => x.dwBytesInRes).FirstOrDefault(); //Get icon bitmap data byte[] iconBytes = GetResourceDataBytesFromIntPtr(libraryHandle, (IntPtr)iconDirEntry.nIdentifier, ResourceTypes.ICON); //Encode icon bitmap frame if (iconBytes[0] == 0x28) { //Debug.WriteLine("BMP image: " + iconBytes.Length); //Create icon from the resource iconHandle = CreateIconFromResourceEx(iconBytes, (uint)iconBytes.Length, true, IconVersion.Windows3x, iconDirEntry.bWidth, iconDirEntry.bHeight, IconResourceFlags.LR_DEFAULTCOLOR); //Convert image data to bitmap Bitmap bitmapImage = Icon.FromHandle(iconHandle).ToBitmap(); //Write bitmap to memorystream bitmapImage.Save(imageMemoryStream, ImageFormat.Png); imageMemoryStream.Seek(0, SeekOrigin.Begin); return(imageMemoryStream); } else { //Debug.WriteLine("PNG image: " + iconBytes.Length); using (MemoryStream memoryStream = new MemoryStream(iconBytes)) { //Convert image data to bitmap Bitmap bitmapImage = new Bitmap(memoryStream); //Write bitmap to memorystream bitmapImage.Save(imageMemoryStream, ImageFormat.Png); imageMemoryStream.Seek(0, SeekOrigin.Begin); return(imageMemoryStream); } } } catch (Exception ex) { Debug.WriteLine("Failed to load exe icon: " + ex.Message); return(null); } finally { if (libraryHandle != IntPtr.Zero) { FreeLibrary(libraryHandle); } if (iconHandle != IntPtr.Zero) { DestroyIcon(iconHandle); } } }
// this method is too long, I'm sorry... private void loadFromLibrary( string libraryFile, string resourceName ) { string msg = ""; bool failed = false; IntPtr hGlobal = IntPtr.Zero; IntPtr hRsrc = IntPtr.Zero; IntPtr hLibrary = IntPtr.Zero; IntPtr lPtr = IntPtr.Zero; try { hLibrary = LoadLibraryEx( libraryFile, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE); if (hLibrary != IntPtr.Zero) { hRsrc = FindResource( hLibrary, resourceName, (IntPtr)RT_GROUP_ICON); if (hRsrc != IntPtr.Zero) { hGlobal = LoadResource(hLibrary, hRsrc); if (hGlobal != IntPtr.Zero) { lPtr = LockResource(hGlobal); if (lPtr != IntPtr.Zero) { // now we can read the header: int iconCount = readResourceIconFileHeader(lPtr); // read the directory: MEMICONDIRENTRY[] ide = new MEMICONDIRENTRY[iconCount]; int ofs = 6; for (int iconEntry = 0; iconEntry < iconCount; iconEntry++) { ide[iconEntry] = new MEMICONDIRENTRY(lPtr, ofs); //Console.WriteLine(ide[iconEntry].ToString()); ofs += 14; } FreeResource(hGlobal); hGlobal = IntPtr.Zero; // we have the directory, so now can load the icons: //IconDeviceImage[] icons = new // IconDeviceImage[iconCount]; ArrayList icons = new ArrayList(); // read the icons: for (int iconEntry = 0; iconEntry < iconCount; iconEntry++) { // find the specified icon: string resName = String.Format("#{0:G}", //N0 ide[iconEntry].nID); hRsrc = FindResource( hLibrary, resName, (IntPtr)RT_ICON); if (hRsrc == IntPtr.Zero) { msg = String.Format( "Could not find the component icon resource with id {0}", ide[iconEntry].nID); failed = true; break; } else { // load the resource: hGlobal = LoadResource( hLibrary, hRsrc); if (hGlobal == IntPtr.Zero) { msg = String.Format( "Could not load the component icon resource with id {0}", ide[iconEntry].nID); failed = true; break; } else { // check the size: int resSize = SizeofResource(hLibrary, hRsrc); if ((resSize > 0) && (resSize == ide[iconEntry].dwBytesInRes)) { // ok lPtr = LockResource(hGlobal); byte[] b = new byte[resSize]; Marshal.Copy(lPtr, b, 0, resSize); try { //icons[iconEntry] = new IconDeviceImage(b); icons.Add(new IconDeviceImage(b)); } catch{} } else { msg = String.Format( "Component icon resource with id {0} is corrupt", ide[iconEntry].nID); failed = true; } } } } if (!failed) { // Add the icons to the collection: IconDeviceImage[] temp = new IconDeviceImage[icons.Count]; icons.CopyTo(temp); this.iconCollection = new IconDeviceImageCollection(temp); } } else { msg = "Can't lock resource for reading."; failed = true; } } else { msg = "Can't load resource for reading."; failed = true; } } else { msg = "Can't find resource."; failed = true; } } else { msg = "Can't load library."; failed = true; } } catch (Exception ex) { failed = true; msg = ex.Message; } finally { // clear up handles: if (hGlobal != IntPtr.Zero) { FreeResource(hGlobal); } if (hLibrary != IntPtr.Zero) { FreeLibrary(hLibrary); } if (failed) { throw new IconExException(msg); } } }