Transparent Gifs with GDI+ / System.Drawing
page 1 of 1
Published: 05 Dec 2003
Unedited - Community Contributed
Creating transparent GIF images with GDI+ and System.Drawing.
by Chris Garrett
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 10475/ 21

Transparent Gifs with GDI+ / System.Drawing
Transparent Gifs with GDI+ / System.Drawing
by Chris Garrett

Wow, this has taken quite a while to work out. For anyone who did this ages ago, just call me Mr Thicky but it has taken a KB article to crack this one for me so thankyou Microsoft for finally dishing out the info!

First of all, let's take a look at the example before you get too excited.


You will notice a distinct lack of dithering and a transparent background - wohoo! :O) 

View Source

This source code is a monster in comparison to my other examples, but most of it should be pretty straightforward to understand, other bits I have cobbled together from the KB article linked to below.

The project is split into three elements, the aspx (which is pretty much just a line to point to the codebehind file), the code behind .vb, plus another "win32api" class which is used to copy the image data (also in the .vb).

Our image is created in the usual way and displayed in the usual way - see past GDI+ and System.Drawing articles for how to do this. The clever bit happens in the function recolorGif which takes a source image and returns the image with the colour palette converted.

As I am pretty lazy my colour-reduction algorithm consists of selecting from a few basic colours ;O) I am sure you will be able to do a better job - the KB article shows how to convert to grey scale.

A couple of temp bitmaps are created, one 1x1 pixels with an indexed palette that returns a clean palette and another with a full colour palette used to draw our source image and copy the image data with new palette.

Check out the link below for a fuller explaination of the whys and hows ;O)



See the KB article here

User Comments

Title: Dang it   
Name: Grrrr
Date: 2008-04-23 10:09:05 AM
This is axatly what I needed... to bad ALL the links to code point to files that have been removed from the server.
Title: Drawing an image on top of a transparent background   
Name: Alan
Date: 2007-10-05 7:32:07 PM
I am trying to put an image (with a texture brush) on top of the transparent background. So that it will show a little space around this image which is trasparent. I been having no luck doing that since everything will just become white/transparent. Any help would be appreciated . Thanks
Title: Regarding Codebook Chapter 22   
Name: Jacob
Date: 2005-10-21 6:58:37 PM
Hey Chris,

I'm assuming you contributed a more polished version of this code for the ASP Alliance Codebook ( I wanted to point out a slight error in that code. The line:

If ColorTranslator.ToHtml(pixel) = "#ffffff" Then

Should actually be "#FFFFFF" - its case sensitive and doesn't work without the caps... Thanks for the work though!

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-23 7:29:35 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search