void LoadIcon(Stream stream) { BinaryReader r = new BinaryReader(stream); if (r.ReadUInt16() != 0) { throw new InvalidIconException("This is not a valid .ico file."); } ushort type = r.ReadUInt16(); if (type == 1) { isCursor = false; } else if (type == 2) { isCursor = true; } else { throw new InvalidIconException("This is not a valid .ico file."); } IconEntry[] icons = new IconEntry[r.ReadUInt16()]; for (int i = 0; i < icons.Length; i++) { icons[i] = new IconEntry(); icons[i].ReadHeader(r, isCursor, ref wellFormed); } for (int i = 0; i < icons.Length; i++) { icons[i].ReadData(stream, ref wellFormed); } // need to use List<T> so that the collection can be resized this.icons = new Collection <IconEntry>(new List <IconEntry>(icons)); }
/// <summary> /// Gets the icon entry with the specified width and height. /// </summary> /// <param name="size">Width and height of the entry to get.</param> /// <param name="bestSupported">The "best" entry type allowed to get. /// Use Compressed to get all entries; TrueColor to get all except /// the Vista-only compressed entries; and Classic to get only the /// classic icons that run on all Windows versions. /// </param> /// <returns>Gets the entry matching the size. If multiple supported /// entries are available, the only with the highest color depth is returned. /// If no matching entry is found, null is returned.</returns> public IconEntry GetEntry(Size size, IconEntryType bestSupported) { IconEntry best = null; foreach (IconEntry e in this.Icons) { if (e.Size == size && e.Type <= bestSupported) { if (best == null || best.ColorDepth < e.ColorDepth) { best = e; } } } return(best); }
/// <summary> /// Adds the specified entry /// </summary> public void AddEntry(IconEntry entry) { if (entry == null) { throw new ArgumentNullException("entry"); } // replace matching existing entry: for (int i = 0; i < icons.Count; i++) { if (icons[i].Width == entry.Width && icons[i].Height == entry.Height && icons[i].ColorDepth == entry.ColorDepth) { icons[i] = entry; return; } } icons.Add(entry); }
void SetImage(Bitmap newBitmap) { // scale to correct size and make it ARGB if (iconSize != newBitmap.Size) { int r = MessageService.ShowCustomDialog( "Import Image", string.Format("The image has size {0}x{1}, but size {2}x{3} is expected.", newBitmap.Width, newBitmap.Height, iconSize.Width, iconSize.Height), 0, 1, "Convert", "Cancel"); if (r != 0) { return; } } bool?compress = entry != null ? entry.IsCompressed : (bool?)null; this.Entry = new IconEntry(iconSize.Width, iconSize.Height, colorDepth, newBitmap, compress); EntryChanged(this, EventArgs.Empty); }
void AddFormatButtonClick(object sender, EventArgs e) { if (activeIconFile == null) { return; } using (PickFormatDialog dlg = new PickFormatDialog()) { if (dlg.ShowDialog() == DialogResult.OK) { int width = dlg.IconWidth; int height = dlg.IconHeight; int colorDepth = dlg.ColorDepth; var sameSizeEntries = activeIconFile.Icons.Where(entry => entry.Width == width && entry.Height == height); if (sameSizeEntries.Any(entry => entry.ColorDepth == colorDepth)) { MessageService.ShowMessage("This icon already contains an image with the specified format.", "Icon Editor"); return; } IconEntry sourceEntry = sameSizeEntries.OrderByDescending(entry => entry.ColorDepth).FirstOrDefault(); if (sourceEntry == null) { sourceEntry = (from entry in activeIconFile.Icons orderby entry.Width descending, entry.Height descending, entry.ColorDepth descending select entry).FirstOrDefault(); } // sourceEntry can still be null if the icon is completely empty Bitmap sourceBitmap = sourceEntry != null?sourceEntry.ExportArgbBitmap() : new Bitmap(width, height); activeIconFile.AddEntry(new IconEntry(width, height, colorDepth, sourceBitmap)); if (IconWasEdited != null) { IconWasEdited(this, e); } ShowFile(activeIconFile); } } }
void CompressedToolStripMenuItemClick(object sender, EventArgs e) { // Toggle compression this.Entry = new IconEntry(iconSize.Width, iconSize.Height, colorDepth, entry.ExportArgbBitmap(), !entry.IsCompressed); EntryChanged(this, EventArgs.Empty); }
void DeleteToolStripMenuItemClick(object sender, EventArgs e) { this.Entry = null; EntryChanged(this, e); }
void SetImage(Bitmap newBitmap) { // scale to correct size and make it ARGB if (iconSize != newBitmap.Size) { int r = MessageService.ShowCustomDialog( "Import Image", string.Format("The image has size {0}x{1}, but size {2}x{3} is expected.", newBitmap.Width, newBitmap.Height, iconSize.Width, iconSize.Height), 0, 1, "Convert", "Cancel"); if (r != 0) { return; } } bool? compress = entry != null ? entry.IsCompressed : (bool?)null; this.Entry = new IconEntry(iconSize.Width, iconSize.Height, colorDepth, newBitmap, compress); EntryChanged(this, EventArgs.Empty); }
void CompressedToolStripMenuItemClick(object sender, EventArgs e) { // Toggle compression this.Entry = new IconEntry(iconSize.Width, iconSize.Height, colorDepth, entry.ExportArgbBitmap(), !entry.IsCompressed); EntryChanged(this, EventArgs.Empty); }
void DeleteToolStripMenuItemClick(object sender, EventArgs e) { this.Entry = null; EntryChanged(this, e); }
/// <summary> /// Adds the specified entry /// </summary> public void AddEntry(IconEntry entry) { if (entry == null) throw new ArgumentNullException("entry"); // replace matching existing entry: for (int i = 0; i < icons.Count; i++) { if (icons[i].Width == entry.Width && icons[i].Height == entry.Height && icons[i].ColorDepth == entry.ColorDepth) { icons[i] = entry; return; } } icons.Add(entry); }
void LoadIcon(Stream stream) { BinaryReader r = new BinaryReader(stream); if (r.ReadUInt16() != 0) throw new InvalidIconException("This is not a valid .ico file."); ushort type = r.ReadUInt16(); if (type == 1) isCursor = false; else if (type == 2) isCursor = true; else throw new InvalidIconException("This is not a valid .ico file."); IconEntry[] icons = new IconEntry[r.ReadUInt16()]; for (int i = 0; i < icons.Length; i++) { icons[i] = new IconEntry(); icons[i].ReadHeader(r, isCursor, ref wellFormed); } for (int i = 0; i < icons.Length; i++) { icons[i].ReadData(stream, ref wellFormed); } // need to use List<T> so that the collection can be resized this.icons = new Collection<IconEntry>(new List<IconEntry>(icons)); }