65.9K
CodeProject is changing. Read more.
Home

VB.Net GIF Transparency

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.28/5 (10 votes)

Aug 8, 2006

1 min read

viewsIcon

65937

downloadIcon

1487

A method of creating transparent GIF image from a source GIF image.

Introduction

I have searched the web (via Google) for examples on how to convert a GIF image into a transparent GIF image without much success. What I wanted was there, just not in the way I wanted it.

Some of the methods I've seen on the web that converts a non-transparent GIF to a transparent GIF used unsafe code to perform the conversions. With luck, I stumbled across a couple of examples that performed the conversion without unsafe code, and I ran into Bob Powell's transparency page and voila, found what I was looking for. Although what I was looking for was there, it was mixed in with the UI code and not put together in a way I could understand. So I took Bob's code, moved the code that did the conversion into it's own class, put some extra comments in there, re-formatted the code, and changed some variable names.

Using the code

The GifConverter class contains a single public shared method, GetTransparentGif. This class takes two arguments, Image (Bitmap) and TransparentColor (Color). The Image argument is the image to be converted. The original image is not touched, instead the image is "copied" to a new image that the method returns. The TransparentColor argument is the color within the palette that will be set as the color to make transparent. There are currently no checks within the method to verify the source image is a GIF image.

Points of Interest

Instead of using unsafe code to perform the copy, this library uses the System.Runtime.Marshal class to perform bit-by-bit reading/writing to effectively copy the source bitmap's data to the destination bitmap. I learned a lot rewriting the original by Bob Powell into a usable library.

History

v1.0 Initial Release