Skip to content

Commit

Permalink
Fixed a bug when ICO couldn't be converted to BMP
Browse files Browse the repository at this point in the history
If an ICO couldn't be converted to a bitmap, it caused all icons to fail being added to the iconselector list. Noticed in Visual Studio 2017 (devenv.exe). Note, this was a fairly rushed fix, may need revisiting in future!
  • Loading branch information
Jonno12345 committed Mar 7, 2018
1 parent 8c52519 commit 2f2b8d9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
15 changes: 15 additions & 0 deletions TileIconifier/Controls/IconListView/IconListVewItemCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ public void Add(IconListViewItem item)
_owner.OnItemAddedInternal(true);
}

public void AddRange(List<IconListViewItem> items)
{
if(items == null)
{
throw new ArgumentNullException(nameof(items));
}

var newItems = new IconListViewItem[items.Count];
for(var i = 0; i < items.Count; i++)
{
newItems[i] = items[i];
}
AddRange(newItems);
}

public void AddRange(IconListViewItem[] items)
{
if (items == null)
Expand Down
24 changes: 16 additions & 8 deletions TileIconifier/Forms/Shared/FrmIconSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private FrmIconSelector(string targetPath)
SetUpOpenFileDialog();
SetUpCommonDllComboBox();
SetUpTargetPath(targetPath);
BuildListView();
BuildListView();
}

public static IconSelectorResult GetImage(IWin32Window owner, string defaultPathForIconExtraction = "")
Expand Down Expand Up @@ -194,14 +194,14 @@ private void BuildListView()
{
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)};
_icons = new[] { new Icon(targetPath) };
}
else
{
Expand All @@ -210,17 +210,25 @@ private void BuildListView()
}

//Build the list view items
var items = new IconListViewItem[_icons.Length];
var items = new List<IconListViewItem>();
for (var i = 0; i < _icons.Length; i++)
{
var splitIcons = IconUtil.Split(_icons[i]);

var largestIcon = splitIcons.OrderByDescending(k => k.Width)
.ThenByDescending(k => Math.Max(k.Height, k.Width))
.First();

var bmp = IconUtil.ToBitmap(largestIcon);
items[i] = new IconListViewItem(bmp);
Bitmap bmp = null;
try
{
bmp = IconUtil.ToBitmap(largestIcon);
}
catch
{
//icon failed to convert to bitmap
continue;
}
items.Add(new IconListViewItem(bmp));

//Icon cleanup
_icons[i].Dispose();
Expand All @@ -230,7 +238,7 @@ private void BuildListView()
}
lvwIcons.Items.AddRange(items);
}
catch
catch (Exception ex)
{
// ignored
}
Expand Down

0 comments on commit 2f2b8d9

Please sign in to comment.