Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround for the ImageList transparency bug?

Tags:

.net

winforms

If you add an PNG image to an ImageList control and use that ImageList with a ListView or TreeView in a Windows Forms (.Net 2.) application the icons shown in the ListView have a blue "halo" around them.

Eg:

alt text
(source: wordpress.com)

Does anyone know a work around that allows you to add 32bit PNGs with an Alpha channel to an ImageList and retain the transparent pixels and avoid the halo effect/bug?

Thanks.

like image 588
orj Avatar asked Mar 06 '09 01:03

orj


1 Answers

I don't think that this is a bug. You should have the ImageList color depth set to 32-bit, and you need to be using visual styles.

If the application has visual styles enabled, WinForms uses version 6.0 of Windows common controls, which supports alpha transparency. Otherwise it will use the previous version which does not support alpha-transparency, and incorrectly renders ARGB images with a blue halo.

If you need a workaround, you could try creating new bitmaps the same size, filling them with the BackColor of the ListView, creating a Graphics object on the bitmap, and drawing your ARGB image onto the bitmap. Then you can insert these pre-blened bitmaps into the ImageList. If you do this, however, the selection will look funny on the icon in the ListView.

Another work-around would be to directly access the bitmap data (using LockBits()) and manually blend any semi-transparent pixels, which will cause the images to be rendered properly and look pretty much the way they should when selected.

like image 82
snarf Avatar answered Nov 06 '22 00:11

snarf