public Icon GetBestIcon(String path) { if (!path.EndsWith(".exe") && !path.EndsWith(".dll")) { return(null); } IconExtractor ie = new IconExtractor(path); string fileName = ie.FileName; int iconCount = ie.Count; if (iconCount > 0) { Icon[] allIcons = ie.GetAllIcons(); Icon ret = null; foreach (var icon in allIcons) { Icon[] splitIcons = IconUtil.Split(icon); Icon biggestIcon = splitIcons.OrderBy(x => x.Size.Height).Last(); if (ret == null || biggestIcon.Height > ret.Height) { ret = biggestIcon; } } return(ret); } return(null); }
private void BuildListView() { //reset the list view items lvwIcons.Items.Clear(); //validate the path exists var targetPath = txtPathToExtractFrom.Text; if (!File.Exists(targetPath)) { return; } lvwIcons.BeginUpdate(); try { //icon files don't need extraction if (string.Equals(Path.GetExtension(targetPath), ".ico", StringComparison.InvariantCultureIgnoreCase)) { _icons = new[] { new Icon(targetPath) }; } else { var iconExtraction = new IconExtractor(targetPath); _icons = iconExtraction.GetAllIcons(); } foreach (var i in _icons) { var splitIcons = IconUtil.Split(i); var largestIcon = splitIcons.OrderByDescending(k => k.Width) .ThenByDescending(k => Math.Max(k.Height, k.Width)) .First(); var item = new IconListViewItem { Bitmap = IconUtil.ToBitmap(largestIcon) }; i.Dispose(); largestIcon.Dispose(); lvwIcons.Items.Add(item); } } catch { // ignored } finally { lvwIcons.EndUpdate(); } }
private void BuildListView() { //reset the list view items lvwIcons.Items.Clear(); //validate the path exists var targetPath = txtPathToExtractFrom.Text; if (!File.Exists(targetPath)) { return; } try { //Get the icons //icon files don't need extraction if (string.Equals(Path.GetExtension(targetPath), ".ico", StringComparison.InvariantCultureIgnoreCase)) { _icons = new[] { new Icon(targetPath) }; } else { var iconExtraction = new IconExtractor(targetPath); _icons = iconExtraction.GetAllIcons(); } //Build the list view items var items = new List <IconListViewItem>(); foreach (Icon icon in _icons) { var splitIcons = IconUtil.Split(icon); var largestIcon = splitIcons.OrderByDescending(k => k.Width) .ThenByDescending(k => Math.Max(k.Height, k.Width)) .First(); Bitmap bmp; try { bmp = IconUtil.ToBitmap(largestIcon); } catch { //icon failed to convert to bitmap continue; } items.Add(new IconListViewItem(bmp)); //Icon cleanup icon.Dispose(); Array.ForEach(splitIcons, ic => ic.Dispose()); //The listview creates its own copy of the bitmap bmp.Dispose(); } lvwIcons.Items.AddRange(items); } catch (Exception) { // ignored } }
/// <summary> /// This event gets called when an item is dropped on the dock /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DisplayGrid_Drop(object sender, System.Windows.DragEventArgs e) { //TODO : We could make it so you are able to add multiple icons at the same time var files = (string[])e.Data.GetData(DataFormats.FileDrop); string filename = files[0]; string startupPath = Environment.CurrentDirectory; System.Drawing.Icon icon = null; string newIconImageName = string.Empty; FileInfo fileInfo = null; string workingDirectory = string.Empty; string text = string.Empty; IconExtractor ie = null; //if (files[0].ToLower().EndsWith("lnk")) //{ // var link = Lnk.Lnk.LoadFile(filename); // fileInfo = new FileInfo(link.LocalPath); // filename = fileInfo.Name; // workingDirectory = link.WorkingDirectory; // text = link.Name; // string iconFile = link.IconLocation; // if (!string.IsNullOrWhiteSpace(iconFile)) // { // int iconIndex = link.Header.IconIndex; // iconFile = Environment.ExpandEnvironmentVariables(iconFile); // if (!string.IsNullOrWhiteSpace(iconFile)) // { // ie = new IconExtractor(iconFile); // icon = ie.GetIcon(iconIndex); // } // } // else // { // icon = System.Drawing.Icon.ExtractAssociatedIcon(link.IconLocation); // } //} //else //{ // if (File.Exists(files[0])) // { fileInfo = new FileInfo(files[0]); filename = fileInfo.Name; text = fileInfo.Name; //Create the image of the Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(files[0]); // } // else if (Directory.Exists(files[0])) // { // newIconImageName = "folder.png"; // } //} if (icon != null && fileInfo != null) { //Create the name that this icon image is going to be stored under newIconImageName = fileInfo.Name.Replace(fileInfo.Extension.ToString(), "") + ".png"; if (!File.Exists(startupPath + "\\icons\\" + newIconImageName)) { IconUtil.ToBitmap(icon).Save(startupPath + "\\icons\\" + newIconImageName); var icons = ie.GetAllIcons(); for (int i = 0; i < icons.Length; i++) { IconUtil.ToBitmap(icons[i]).Save(startupPath + "\\icons\\imageres\\" + i.ToString() + ".png"); } } } // instantiate a new shortcut object temporarily with enough information to generate an XML fragment var button = new Bauble.Buttons.Shortcut("icons/" + newIconImageName, text, filename, workingDirectory) as IBaubleButton; // have the settings object create a fully instantiated button object button = _settings.AddShortcut(button); // load the button onto the current dock DisplayButton(DisplayGrid.ColumnDefinitions.Count + 1, button); // Allow the IconFlow to initialize the button for any animations it uses _settings.IconFlow.Initialize(button); // save the new button list out to the file system _settings.SaveButtonList(); }
public static Bitmap getIconX(string fn) { Icon icon; IconExtractor ie; FileInfo inf = new FileInfo(fn); if (inf.Extension.ToLower() != ".exe") { return(Extends.IconHelper.IconReader.GetFileIcon(fn, IconHelper.IconReader.IconSize.Small, false).ToBitmap()); } try { ie = new IconExtractor(fn); } catch (Exception) { return(null); } // Get the full name of the associated file. string fileName = ie.FileName; // Get the count of icons in the associated file. int iconCount = ie.Count; // Extract icons individually. Icon icon0 = ie.GetIcon(0); // Extract all the icons in one go. Icon[] allIcons = ie.GetAllIcons(); // ----------------------------------------------------------------------------- // Usage of IconUtil class: // Split the variations of icon0 into separate icon objects. Icon[] splitIcons = IconUtil.Split(icon0); // Convert an icon into bitmap. Unlike Icon.ToBitmap() it preserves the transparency. Icon bestOne = null; foreach (Icon i in splitIcons) { if (bestOne == null && i.Height <= Vars.MAXIMUM_ICON_SIZE.Height) { bestOne = i; } if (bestOne != null && i.Height > bestOne.Height && i.Height <= Vars.MAXIMUM_ICON_SIZE.Height) { bestOne = i; } } //Bitmap bitmap = splitIcons[0].ToBitmap(); // Get the bit count of an icon. //int bitDepth = IconUtil.GetBitCount(splitIcons[2]); return((bestOne ?? Properties.Resources.app_icon).ToBitmap()); }