Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Using .ico file with multiple images

Tags:

c#

.net

icons

I'm trying to set the images in a TreeView in C#, from a .ico file containing two icons: a 32x32 version and a 16x16 version. The images are setting, but .Net is choosing the 32x32 version, and scaling it down (which looks terrible) instead of choosing the readily available 16x16 image.

The relevant code:

ilTree.Images.Add(Properties.Resources.group);
ilTree.Images.Add(Properties.Resources.single);
ilTree.Images.Add(Properties.Resources.db);
treeStored.ImageList = ilTree;

Where am I going wrong?


Answer:

There are two things you must do to get this to work. The first, as mentioned below, is to manually specify the correct size to the image list. The second is that you will also probably have to specify the color depth. MSDN states that:

In the .NET Framework version 1.1 or later, the default is Depth8Bit.

...however, that did not prevent ImageList from removing colors from my 8-bit icon. Upon close inspection, my icons (there were three) were: 4bit, 4bit, 8bit. The two 4 bit icons shared palettes, however the 8bit had a different one. In total, there were 257 colors between the icons. Despite the only slight overflow, .Net knocked it down to a mere 20 colors.

like image 323
Thanatos Avatar asked Jun 08 '09 20:06

Thanatos


1 Answers

You need to create a new Icon object. There is an overload for the constructor that accepts the original Icon object and a size. I don't know if the new Icon object will share the same HIcon, but it will draw properly. To be safe, I would recommend ensuring that both are disposed.

like image 83
snarf Avatar answered Sep 27 '22 19:09

snarf