Windows Develop Bookmark and Share   
 index > Windows Forms General > Image transparency in TreeView
 

Image transparency in TreeView

Hi,

I have troubles with transparent icons in a TreeView. Looked into some threads here but yet didn't understand.


Have transparent gif and png bitmaps in project resource file Resources.resx.The Bitmap objects can be referenced by static members like "Resources.image1", am creating Image's and adding them to a TreeView.ImageList like this

Image img = Image.FromHbitmap( Resources.image1.GetHbitmap() );
treeView.ImageList.Add( img );


But the images are not displayed correctly, it seems that regardless of the ImageList.TransparentColor, .ImageSize and .ColorDepth settings which I was experimenting with, the transparent color is replaced by a 0x000000CC blue. EDIT: the transparent color in the original files is a 0x00000000, which I was also settingexplicitlyto ImageList.TransparentColor

If I use the same images with some other components that support a Image property and initialize that from the designer, the transparency is all right.

.NET3.5, WinXP

Please, what could be wrong?

Thanks

andris11  Saturday, July 18, 2009 3:16 PM
Start by not using GetHbitmap(), you are leaking the bitmap handle:

treeView.ImageList.Add(Resources.image1);

And check the ImageList.ColorDepth setting, the default (8Bit) is not appropriate for images with transparency.
Hans Passant.
  • Marked As Answer byandris11 Tuesday, July 28, 2009 4:05 PM
  •  
nobugz  Saturday, July 18, 2009 3:45 PM
Start by not using GetHbitmap(), you are leaking the bitmap handle:

treeView.ImageList.Add(Resources.image1);

And check the ImageList.ColorDepth setting, the default (8Bit) is not appropriate for images with transparency.
Hans Passant.
  • Marked As Answer byandris11 Tuesday, July 28, 2009 4:05 PM
  •  
nobugz  Saturday, July 18, 2009 3:45 PM
Start by not using GetHbitmap(), you are leaking the bitmap handle:

treeView.ImageList.Add(Resources.image1);

sorry but ImageList.Add() doesn't take Bitmaps so that can't work and I don't have the .icon files.
And check the ImageList.ColorDepth setting, the default (8Bit) is not appropriate for images with transparency.
Hans Passant.

I converted the 8-bit gifs to 32 bit png's and played with the ColorDepth settings, but there was no success yet. The bitmaps use the a 0x00000000 for the transparent pixels. I must be missing something, any more ideas?

Thanks a lot

andris11  Wednesday, July 22, 2009 7:14 PM
Yes, it does.
Hans Passant.
nobugz  Wednesday, July 22, 2009 8:31 PM
Hi Andris,

The following article introduces how to make a transparent bitmap, which may help you:
http://www.winprog.org/tutorial/transparency.html
Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Linda Liu  Tuesday, July 28, 2009 10:18 AM
Hi,
the whole trouble was that the following line wasn't compiling for some reason (solved after IDE restart)

treeView.ImageList.Add(Resources.image1);

Voila, now I'm getting correct transparency with 8-bit gifs and 32-bit pngs too. It works regardless of any ColorDepth settings, I even can mix 8-bit and 32-bit images in one ImageList. Same works fine with ToolStripMenuItem.Image for example.

So, everything's cool :) Thanks for help.

andris11  Tuesday, July 28, 2009 4:11 PM

You can use google to search for other answers

Custom Search

More Threads

• 2 Forms... how to..
• How to pass Composite Control / User Control messages to the parent Form?
• C# 3.0 - Change parameters into SQL string retrieved from resources file
• C# + Replace
• Application Settings User Scope
• Open Special Characters & Symbols Window in C#
• Drop Down list of events for a component/form?
• can not get odbc connection to work
• number count down
• toolstripcombobox with scroll bar