Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Eveldee committed Aug 23, 2018
1 parent 8c715cb commit e70fb9c
Show file tree
Hide file tree
Showing 16 changed files with 1,097 additions and 31 deletions.
12 changes: 12 additions & 0 deletions ExeIconPicker.sln
Expand Up @@ -8,13 +8,25 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Debug|x64.ActiveCfg = Debug|x64
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Debug|x64.Build.0 = Debug|x64
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Debug|x86.ActiveCfg = Debug|x86
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Debug|x86.Build.0 = Debug|x86
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Release|Any CPU.Build.0 = Release|Any CPU
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Release|x64.ActiveCfg = Release|x64
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Release|x64.Build.0 = Release|x64
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Release|x86.ActiveCfg = Release|x64
{F947D95B-45AE-46CA-8982-A3E071D434A3}.Release|x86.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
11 changes: 0 additions & 11 deletions ExeIconPicker/Class1.cs

This file was deleted.

175 changes: 175 additions & 0 deletions ExeIconPicker/Controls/BitmapPickerDialog.cs
@@ -0,0 +1,175 @@
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using TsudaKageyu;

namespace ExeIconPicker.Controls
{
public partial class BitmapPickerDialog : Form //source: https://www.codeproject.com/articles/26824/extract-icons-from-exe-or-dll-files
{
public Bitmap Result { get; private set; }
private bool firstOpen = true;

private class IconListViewItem : ListViewItem
{
public Bitmap Bitmap { get; set; }
}

public BitmapPickerDialog()
{
InitializeComponent();
}

private void ClearAllIcons()
{
foreach (var item in lvwIcons.Items)
((IconListViewItem)item).Bitmap.Dispose();

lvwIcons.Items.Clear();
}

private void BitmapPickerDialog_Load(object sender, EventArgs e)
{
btnPickFile_Click(this, null);
firstOpen = false;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
ClearAllIcons();
}

private void btnOk_Click(object sender, EventArgs e)
{
Return_Result();
}

private void BtnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
this.Hide();
}

private void btnPickFile_Click(object sender, EventArgs e)
{
var result = iconPickerDialog.ShowDialog(this);
if (result == 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();
}
}

private void lvwIcons_DrawItem(object sender, DrawListViewItemEventArgs e)
{
var item = e.Item as IconListViewItem;

// Draw item

e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.Clip = new Region(e.Bounds);

if (e.Item.Selected)
e.Graphics.FillRectangle(SystemBrushes.MenuHighlight, e.Bounds);
else
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);

int w = Math.Min(128, item.Bitmap.Width);
int h = Math.Min(128, item.Bitmap.Height);

int x = e.Bounds.X + (e.Bounds.Width - w) / 2;
int y = e.Bounds.Y + (e.Bounds.Height - h) / 2;
var dstRect = new Rectangle(x, y, w, h);
var srcRect = new Rectangle(Point.Empty, item.Bitmap.Size);


e.Graphics.DrawImage(item.Bitmap, dstRect, srcRect, GraphicsUnit.Pixel);

var textRect = new Rectangle(
e.Bounds.Left, e.Bounds.Bottom - Font.Height - 4,
e.Bounds.Width, Font.Height + 2);
TextRenderer.DrawText(e.Graphics, item.ToolTipText, Font, textRect, ForeColor);

e.Graphics.Clip = new Region();
e.Graphics.DrawRectangle(SystemPens.ControlLight, e.Bounds);
}

private void lvwIcons_ItemActivate(object sender, EventArgs e)
{
// Return result when activated
Return_Result();
}

private void Return_Result()
{
// Check if selection is empty
var items = lvwIcons.SelectedItems;
if (items == null || items.Count < 1)
return;

Result = ((IconListViewItem)items[0]).Bitmap.Clone() as Bitmap;
DialogResult = DialogResult.OK;
this.Hide();
}
}
}
126 changes: 126 additions & 0 deletions ExeIconPicker/Controls/BitmapPickerDialog.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e70fb9c

Please sign in to comment.