Transparent image generation

Sometimes you run into a task that should be easy but ends up being hard. Today I hit a big one. I was trying to dynamically generate an image with a transparent background that could be displayed in a web browser using GDI+ in the .NET framework. I was working with GIFs, since I had read previously that IE doesn't support transparent PNGs (unless you use the filter hack). 

At first look it didn't seem hard - create a Bitmap, draw my image, call Bitmap.MakeTransparent, and save the result. Easy...but it didn't work. The image rendered as a solid black rectangle. Hi ho, hi ho, it's off to Google I go.

Unfortunately, my Googling confirmed that lots of other people have encountered the same issue. Apparently when you save the bitmap, the transparency information is lost. The only solution I've found is a nasty chunk of code that does a bunch of low-level byte manipulations to build a new GIF with the correct palette information. Ick. Ugly, and requires creating a second copy of the image.

Since IE doesn't support transparent PNGs and .NET doesn't support transparent GIFs, I'm pretty well screwed. I guess I'll have to use the bitmap rebuilding hack. I hope Microsoft considers this a bug to be fixed in some future version of the framework. Or how about fixing IE to support transparent PNGs? Oh, right, I forgot, IE doesn't need improving anymore.

 

 

 

1 Comment

  • Yes, that's the filter hack that I mentioned. I hadn't run across the behavior that your link references before, though. Thanks, it could be useful. It still limits me to IE 5.5 or later, but that might be good enough.

Comments have been disabled for this content.