LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Transparent Gifs with GDI+ / System.Drawing
by Chris Garrett
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 12727/ 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

Product Spotlight
Product Spotlight 

©Copyright 1998-2020  |  Page Processed at 2020-02-26 5:06:09 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search