tags:

views:

1182

answers:

2

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.

+1  A: 

Set the ImageSize property of the ImageList to new Size(16, 16)

rein
That is the default size of the ImageList - setting it manually would (and has, when I add it) no effect.
Thanatos
hmm, this has previously worked for me. Do you set it before you add the images or afterwards? Didn't realise it was the default.
rein
I tried adding it before. After also has no effect.
Thanatos
+1  A: 

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.

Snarfblam
Well, it works. Although it would certainly be much nicer if TreeView would just do The Right Thing™...
Thanatos
Actually, not quite. This -- ilTree.ColorDepth = ColorDepth.Depth24Bit; -- is also needed. Otherwise, it seems to assume (incorrectly) 16-bit.
Thanatos
The default is 8-bit, but it should load the images with which ever color depth you have previously specified.
Snarfblam