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

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.

/chrisg/tools/transparentgif.aspx

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

View Source
/chrisg/tools/view-transparentgif.vb.asp

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)

Download
/chrisg/tools/download.asp?file=transparentgif.vb

/chrisg/tools/download.asp?file=transparentgif.aspx

See the KB article here



User Comments

Title: Dang it   
Name: Grrrr
Date: 2008-04-23 10:09:05 AM
Comment:
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
Comment:
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
Comment:
Hey Chris,

I'm assuming you contributed a more polished version of this code for the ASP Alliance Codebook (http://aspalliance.com/cookbook/ViewChapter.aspx?Chapter=22). 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 ASPAlliance.com  |  Page Processed at 2024-04-25 10:37:04 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search