Click here to Skip to main content
15,893,904 members
Articles / Web Development / ASP.NET
Article

VB.Net GIF Transparency

Rate me:
Please Sign up or sign in to vote.
3.28/5 (10 votes)
8 Aug 20061 min read 65.3K   1.5K   18   11
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I have worked, on a daily basis, with C#, VB.Net, VB 6, Javascript, VBScript, legacy ASP using Javascript and VBScript, Perl, SQL, Small(C), DHTML, HTML, XML, C and some C++ in *nix environment. Currently, I'm learning COBOL on an IBM mainframe.

My favorite development projects include developing the Windows Form Controls and Web Controls in .Net. Primarily, the design-time classes (Designer and TypeConverters) for controls and classes, and GDI+.

Comments and Discussions

 
GeneralTransparent Background depends on color quantize method Pin
Karl-Werner Rieger7-May-09 12:37
Karl-Werner Rieger7-May-09 12:37 
GeneralDo not work... Pin
abaco1235-Dec-06 3:18
abaco1235-Dec-06 3:18 
GeneralRe: Do not work... Pin
Mythran6-Dec-06 6:49
Mythran6-Dec-06 6:49 
GeneralRe: Do not work... Pin
abaco1237-Dec-06 2:39
abaco1237-Dec-06 2:39 
QuestionTransparent color [modified] Pin
dshorter115-Aug-06 9:09
dshorter115-Aug-06 9:09 
AnswerRe: Transparent color Pin
davehale2318-Sep-07 5:34
davehale2318-Sep-07 5:34 
GeneralWhy not ... Pin
dotnetvirgin8-Aug-06 19:02
dotnetvirgin8-Aug-06 19:02 
GeneralRe: Why not ... Pin
Mythran9-Aug-06 5:38
Mythran9-Aug-06 5:38 
GeneralRe: Why not ... Pin
dotnetvirgin9-Aug-06 12:30
dotnetvirgin9-Aug-06 12:30 
GeneralRe: Why not ... Pin
Mythran14-Aug-06 9:20
Mythran14-Aug-06 9:20 
GeneralRe: Why not ... Pin
dotnetvirgin15-Aug-06 13:23
dotnetvirgin15-Aug-06 13:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.