private void pcIcon_Click(object sender, EventArgs e) { var iconPickerDialog = new IconPickerDialog(); if (iconPickerDialog.ShowDialog(this) == DialogResult.OK) { _iconIndex = iconPickerDialog.IconIndex; var extractor = new IconExtractor(iconPickerDialog.FileName); var icon = extractor.GetIcon(iconPickerDialog.IconIndex); var splitIcons = IconUtil.Split(icon); var maxSizeIcon = splitIcons.Where(f => f.Height == splitIcons.Max(m => m.Height)).ToList(); if (maxSizeIcon.Any()) { pcIcon.Image = IconUtil.ToBitmap(maxSizeIcon[maxSizeIcon.Count() - 1]); } } }
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> /// /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static List <Bitmap> fun_exe_img(String fileName) { List <Bitmap> ar_Bitmap_2 = new List <Bitmap>(); Icon icon = null; Icon[] splitIcons = null; IconExtractor extractor = null; try { extractor = new IconExtractor(fileName); } catch { return(null); } //同一個執行檔,會有很多套不同的icon for (int k = 0; k < extractor.Count; k++) { List <Bitmap> ar_Bitmap = new List <Bitmap>();//該套icon裡面的所有size try { icon = extractor.GetIcon(k); splitIcons = IconUtil.Split(icon); } catch { continue; } // Update icons. foreach (var i in splitIcons) { try { //var size = i.Size; //var bits = IconUtil.GetBitCount(i); ar_Bitmap.Add(IconUtil.ToBitmap(i)); i.Dispose(); } catch { continue; } } //排序(由大到小 for (int i = 0; i < ar_Bitmap.Count; i++) { for (int j = i; j < ar_Bitmap.Count; j++) { if (ar_Bitmap[i].Width < ar_Bitmap[j].Width) { Bitmap bb = ar_Bitmap[i]; ar_Bitmap[i] = ar_Bitmap[j]; ar_Bitmap[j] = bb; } } } //放入要回傳的陣列 foreach (var item in ar_Bitmap) { if (item != null) { if (item.Width > 5) { ar_Bitmap_2.Add(item); } } } }//for return(ar_Bitmap_2); }
private void btnPickFile_Click(object sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Icons (*.exe;*.ico;*.dll;*.lnk)|*.exe;*.ico;*.dll;*.lnk|All files (*.*)|*.*", //DereferenceLinks = true, // This thing doesn't work sadly, idk why Title = "Pick an icon (.ico) or a file containing an icon (.exe, .dll)" }) { if (openFileDialog.ShowDialog() == DialogResult.OK) { FileInfo fileInfo = new FileInfo(openFileDialog.FileName); // Dereference link cause MS can't make it work on OpenFileDialog if (fileInfo.Extension == ".lnk") { iconPickerDialog.FileName = Symlink.ResolveShortcut(fileInfo.FullName); } else { iconPickerDialog.FileName = fileInfo.FullName; } if (iconPickerDialog.ShowDialog(this) == DialogResult.OK) { var fileName = iconPickerDialog.FileName; var index = iconPickerDialog.IconIndex; // Set FileName to further retrieve it FileName = Path.GetFileNameWithoutExtension(fileName); Icon icon = null; Icon[] splitIcons = null; try { if (Path.GetExtension(iconPickerDialog.FileName).ToLower() == ".ico") { icon = new Icon(iconPickerDialog.FileName); } else { var extractor = new IconExtractor(fileName); icon = extractor.GetIcon(index); } splitIcons = IconUtil.Split(icon); } catch (Exception ex) { MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } txtFileName.Text = string.Format("{0}, #{1}, {2} variations", fileName, index, splitIcons.Length); // Update icons. Icon = icon; icon.Dispose(); lvwIcons.BeginUpdate(); ClearAllIcons(); foreach (var i in splitIcons) { // Exclude all icons which size is > 256 (Throw "Generic GDI+ error" when converting if size > 128x128) if (i.Width > 128 || i.Height > 128) { continue; } var item = new IconListViewItem(); var size = i.Size; var bits = IconUtil.GetBitCount(i); item.ToolTipText = string.Format("{0}x{1}, {2} bits", size.Width, size.Height, bits); item.Bitmap = IconUtil.ToBitmap(i); i.Dispose(); lvwIcons.Items.Add(item); } lvwIcons.EndUpdate(); } else if (firstOpen) { DialogResult = DialogResult.Cancel; this.Hide(); } } } }
/// <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(); }
private void btnPickFile_Click(object sender, EventArgs e) { if (iconPickerDialog.ShowDialog(this) == DialogResult.OK) { var fileName = iconPickerDialog.FileName; var index = iconPickerDialog.IconIndex; Icon icon = null; Icon[] splitIcons = null; try { if (Path.GetExtension(iconPickerDialog.FileName).ToLower() == ".ico") { icon = new Icon(iconPickerDialog.FileName); } else { var extractor = new IconExtractor(fileName); icon = extractor.GetIcon(index); } splitIcons = IconUtil.Split(icon); } catch (Exception ex) { MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } txtFileName.Text = String.Format("{0}, #{1}, {2} variations", fileName, index, splitIcons.Length); // Update icons. Icon = icon; icon.Dispose(); lvwIcons.BeginUpdate(); ClearAllIcons(); foreach (var i in splitIcons) { // Exclude all icons which size is > 256 (Throw "Generic GDI+ error" when converting if size > 128x128) if (i.Width > 128 || i.Height > 128) { continue; } var item = new IconListViewItem(); var size = i.Size; var bits = IconUtil.GetBitCount(i); item.ToolTipText = String.Format("{0}x{1}, {2} bits", size.Width, size.Height, bits); item.Bitmap = IconUtil.ToBitmap(i); i.Dispose(); lvwIcons.Items.Add(item); } lvwIcons.EndUpdate(); } else if (firstOpen) { DialogResult = DialogResult.Cancel; this.Hide(); } }
/// <summary> /// Gets the file icon of an .exe /// </summary> /// <param name="fileLocation"></param> /// <returns></returns> private ImageSource GetFileIcon(string fileLocation) { //extract icon IconExtractor iconExtractor = new IconExtractor(fileLocation); //get main icon Icon icon = iconExtractor.GetIcon(0); //split the icon (as there are multiple sizes embeded) Icon[] splitIcons = IconUtil.Split(icon); //dispose of the original icon object since we no longer need it icon.Dispose(); //the highest quality icon, will be assigned later Icon iconHQ; //pixel width, will be changed through iterations and compared int width = 0; //the index of the highest quality one we find in the list int hqIndex = 0; //run a loop through the icon list to get highest quality icon for (int i = 0; i < splitIcons.Length; i++) { //if the pixel width is bigger than our width variable then assign it if (splitIcons[i].Width > width) { //assign the pixel width (used for comparison later) width = splitIcons[i].Width; //get the index of the element hqIndex = i; } } //if the loop finds the one with the highest quality, then use it using the hq index iconHQ = splitIcons[hqIndex]; //convert the icon to a bitmap Bitmap bitmap = IconUtil.ToBitmap(iconHQ); //create a temporary path for it string tempPath = appSettings.Get_App_ConfigDirectory() + "tempIconPreview.bmp"; //if there is already a file of the same name, get rid of it if (System.IO.File.Exists(tempPath)) { IOManagement.DeleteFile(tempPath); } //save the bitmap to the temp path we defined bitmap.Save(tempPath); //clear the original object if it isn't already if (bitmapimage != null) { bitmapimage = null; } //initallize our bitmap image object with a new one (not the same as the bitmap we defined prior) bitmapimage = new BitmapImage(); bitmapimage.BeginInit(); //initalize the image bitmapimage.UriSource = new Uri(tempPath, UriKind.Absolute); //set the path bitmapimage.CacheOption = BitmapCacheOption.OnLoad; //cache the image since we only need it once bitmapimage.EndInit(); //end the initalization //return the final image return(bitmapimage); }
private void btnPickFile_Click(object sender, EventArgs e) { var result = iconPickerDialog.ShowDialog(this); if (result == DialogResult.OK) { var fileName = iconPickerDialog.FileName; m_iconIndex = iconPickerDialog.IconIndex; Icon icon = null; Icon[] splitIcons = null; try { if (Path.GetExtension(iconPickerDialog.FileName).ToLower() == ".ico") { m_iconExtractor = null; icon = new Icon(iconPickerDialog.FileName); } else { m_iconExtractor = new IconExtractor(fileName); icon = m_iconExtractor.GetIcon(m_iconIndex); } splitIcons = IconUtil.Split(icon); } catch (Exception ex) { MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } txtFileName.Text = String.Format( "{0}, #{1}, {2} variations", fileName, m_iconIndex, splitIcons.Length); // Update icons. Icon = icon; icon.Dispose(); lvwIcons.BeginUpdate(); ClearAllIcons(); int it = 0; foreach (var i in splitIcons) { var item = new IconListViewItem(); var size = i.Size; item.BitCount = IconUtil.GetBitCount(i); item.Bitmap = IconUtil.ToBitmap(i); item.ToolTipText = String.Format("{0}x{1}, {2} bits" + "," + it, size.Width, size.Height, item.BitCount); i.Dispose(); lvwIcons.Items.Add(item); it++; } lvwIcons.EndUpdate(); btnSaveAsIco.Enabled = (m_iconExtractor != null); } }