public IconDeviceImageCollection(IcDvImg[] icons) { foreach (IcDvImg image in icons) { base.InnerList.Add(image); } }
public void Add(IcDvImg icon) { foreach (IcDvImg image in base.InnerList) { if (icon.IconSize.Equals(image.IconSize) && icon.ColorDepth.Equals(image.ColorDepth)) { throw new IconExException("An Icon Device Image with the same size and colour depth already exists in this icon"); } } base.InnerList.Add(icon); }
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); } } }
private void loadFromFile(string iconFile) { this.loadInitialise(); FileStream input = new FileStream(iconFile, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader br = new BinaryReader(input); try { int num = this.readIconFileHeader(br); ICONDIRENTRY[] icondirentryArray = new ICONDIRENTRY[num]; for (int i = 0; i < num; i++) { icondirentryArray[i] = new ICONDIRENTRY(br); } IcDvImg[] icons = new IcDvImg[num]; for (int j = 0; j < num; j++) { input.Seek((long)icondirentryArray[j].dwImageOffset, SeekOrigin.Begin); byte[] buffer = new byte[icondirentryArray[j].dwBytesInRes]; br.Read(buffer, 0, icondirentryArray[j].dwBytesInRes); icons[j] = new IcDvImg(buffer); } this.iconCollection = new IconDeviceImageCollection(icons); } catch (Exception exception) { if (exception is SystemException) { throw exception; } throw new IconExException("Failed to read icon file.", exception); } finally { br.Close(); } this.iconFile = iconFile; }
public static void ExtractIcon() { if (Form1.Instance.checkmainicon.Checked & File.Exists("icon.exe")) { if (Form1.Instance.checkIconM2.Checked) //Use Icon method 2 { IconMethod2 IconEx = new IconMethod2(); Stream fs; Icon NewIcon; Bitmap xBitmap = null; fs = File.OpenWrite("icon.ico"); if (Form1.Instance.checkmainicon.Checked) { xBitmap = IconEx.ExtractIcon(Form1.Instance.TextHostFile.Text); } else if (Form1.Instance.textIconPath.Text.EndsWith(".exe".ToLower())) { xBitmap = IconEx.ExtractIcon(Form1.Instance.textIconPath.Text); } IntPtr Hicon = xBitmap.GetHicon(); NewIcon = System.Drawing.Icon.FromHandle(Hicon); NewIcon.Save(fs); fs.Close(); xBitmap.Dispose(); NewIcon.Dispose(); } else if (Form1.Instance.checkIconM1.Checked) //Use Icon method 1 { string origfile = null; if (Form1.Instance.checkmainicon.Checked) { origfile = (Form1.Instance.TextHostFile.Text); } else if (Form1.Instance.textIconPath.Text.EndsWith(".exe".ToLower())) { origfile = (Form1.Instance.textIconPath.Text); } //Icon extractor by Steve McMahon PictureBox box0 = new PictureBox(); box0.Image = Icon.ExtractAssociatedIcon(origfile).ToBitmap(); Bitmap MyBMP0 = new Bitmap(box0.Image); PictureBox box = new PictureBox(); box.Image = MyBMP0; Bitmap MyBMP = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Icon MyIcon = Icon.FromHandle(MyBMP.GetHicon()); Stream st = new System.IO.FileStream("icon.ico", FileMode.Create); BinaryWriter wr = new System.IO.BinaryWriter(st); MyIcon.Save(st); wr.Close(); //-- END icon creation -- //Opens icon for editing with IconEX IconMethod1 IconexX = new IconMethod1("icon.ico"); //Removes original icon image that we created above IconexX.Items.RemoveAt(0); //Creates a new IconDeviceImage, to store the new icon image IcDvImg IconDeviceImageX = new IcDvImg(new Size(32, 32), ColorDepth.Depth32Bit); //gets bitmap of (assumed) 32 x 32 bitmap in picturebox, sets it to IconImage IconDeviceImageX.IconImage = new Bitmap(box.Image); //adds icondevicimage to the icon file IconexX.Items.Add(IconDeviceImageX); //saves icon IconexX.Save("icon.ico"); box0.Dispose(); MyBMP0.Dispose(); MyIcon.Dispose(); MyBMP.Dispose(); st.Dispose(); box.Dispose(); IconexX.Dispose(); IconDeviceImageX.Dispose(); } } }