How to display Gif89a with transparent color other than black

I would like to know how can I display a GIF89a file with tranparent color other than black. The popular browsers do it but I want to do this in my own program written in C++ for Windows 95 (and higher). If transparent color is black I made a mask for image replacing all black pixels by white ones and the others by the white ones and next I used two BitBlt's, the first with the mask and AND ROP-code and the second with the image and the OR ROP-code. But if a transparent color is other than black this method obviously doesn't work. Nevertheless the browsers know how to resolve this problem.  Do you know any method for C++ Windows program ?
Thank you in advance for any response
Who is Participating?

Improve company productivity with a Business Account.Sign Up

nils pipenbrinckConnect With a Mentor Commented:
Yes, you can do that.

I don't know how you load your gif-files.. I think you use a third party library for that.. take a look at the documentation how to get the transparent color index.

If the library don't support this, you can write a code which opens the gif yourself.. just scan the header for the extension flag and extract the transparent color index (if present). You only need the header, the hard part (decoding the image data) can still be done by your existing code.

here is a documentation of the fileformat:

After you know which of the 256 colors is your transparent color, you can simply build your and/or mask based on that color. It's nothing more than a simple loop which iterates over the pixels and sets the pixel in the mask based on the pixel color... A quite easy job I think.

Hope that helps.. I cannot give you sourcecode, because I've never dealed with transparent gif files myself..


  Nils Pipenbrinck
rainetteAuthor Commented:
I load GIF-files with my own procedure which is slightly modified code from Nabayoti Barcacati's book on Borland C++ programming. But WHAT IS "A THIRD PARTY LIBRARY" ? Thanks to your hint I think that I found a better one: instead to change pixels I will change color values in appropriate color palette place.
( GIF-files I know always use color palettes. )
For image I'll change color value for transparent color index to black ( RGB(0,0,0) ), for mask - I'll change this value to white( RGB(255,255,255) ) and all the other values to black. Is it a good idea ?
nils pipenbrinckCommented:
changing the palette-entry is a good idea.. and it'll always work since there aren't any gif files without palette.

what's a third party library? that's a library you buy don't write yourself but buy somewhere... Like for example you want to use a library that handles thousands of fileformats you'll better buy something instead of spending a year developing and debugging it yourself.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.