Tuesday, March 1, 2011

ImageList Transparency not working at runtime

I am using 32bit PNG files with transparency. I added them to an image list with properties:

ColorDepth: Depth32Bit
TransparentColor: Transparent

When I assign the image to my toolbar button, it previews in Visual Studio fine with the correct transparency. But when I run the application the transparency is all messed up with black covering the semi-transparent regions.

How do I fix this?

alt text

alt text

From stackoverflow
  • If you add them directly to the toolbar without using an image list I think they will draw correctly.

    OregonGhost : Easy solution. I also prefer this way.
    Jack B Nimble : Well I would prefer to use an image list to make it easy to swap the images of the toolbar buttons (like the toggle inactive button).
    Matt Breckon : I agree but I've never managed to get this to work.
    OregonGhost : In my opinion, you shouldn't change a toolbar button's text or icon at runtime. You can use a checkable button for that.
    Jack B Nimble : How do I add them directly to the toolbar?
    OregonGhost : I guess you are using SWF 1.1 style toolbar? I suggest using the 2.0 style ToolStrip instead. This not only adds some shine to your application, but also enables setting the images directly :)
  • The solution apparently is to put

    Application.EnableVisualStyles();
    

    Before the start of your application. This has fixed the transparency issue.

    Solution Reference

    MSDN on EnableVisualStyles

    Matt Breckon : Ok, I have seen this problem occur before without that resolving it. I did wonder why your second screenshot looked so chunky!

0 comments:

Post a Comment