C# – Preventing color bleeding with StateImageList on TreeView

cnettreeviewwinforms

Does anyone have a solution for the color bleeding when using a StateImageList with a Windows Forms TreeView?

Using the same ImageList with assigned to the TreeView's ImageList property, results in correct rendering as can be seen by the following image.

24 bit

The state images are left, normal images are right. All images are sourced from the same ImageList instance.

Update:

I converted the images to 16 color indexed palette. Seems it does not like that either. Its a bit better, but still far from acceptable.

4 bit

Best Answer

The documentation on the .ColorDepth property of the .StateImageList states:

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

It looks like this 'default' is not allowed to be changed. Well, you can change it, but it doesn't do any good.

Looking at the code in Reflector, when the StateImageList property of the the TreeView is set, a new image list is created internally. This image list uses the default value for color depth. And since you cannot change the color depth after creating the imagelist, this pretty much guarantees that the StateImageList can only be displayed at the default color depth.

EDIT: I think I've figured out what's going on here. I have been playing around with images like your shaded circles at varying color depths. Basically, when I take a decent image and convert it to 4-bit color, I get results that look exactly like the images from the StateImageList. Despite the '8-bit' note in the documentation, it appears that the StateImageList is still using 4-bit color depth (the old, old 16-color standard) as the default.

It is only when I use 4-bit images to start with in the imagelist, that I get no changes between the display of the imagelist and stateimagelist.

alt text http://www.freeimagehosting.net/uploads/74873abc94.png

I created this image for the imagelist using the old Windows 16-color palette. It looks absolutely terrible, but it didn't change in the stateimagelist.