1
votes

I am trying to save a GIF with transparency in GDI+, but it seems to reuse the first color in the color table - is this a bug with GDI?

Even if I manually set the colour and resave a gif to a gif, I can't ever get it to output a transparent gif to file:

Bitmap b = new Bitmap("c:\\temp\\source.gif");

Bitmap canvas = new Bitmap(b.Width, b.Height);

Graphics g = Graphics.FromImage(canvas);
g.Clear(Color.Transparent);

// Draw image
g.DrawImage(b, 0, 0);

canvas.MakeTransparent(Color.Black);
canvas.Save("c:\\temp\\output.gif", System.Drawing.Imaging.ImageFormat.Gif); 

In the output image black is never set as the transparency colour.

Alternatively is there a way to do this in WPF?

1
It is just a clumsy old image format with entirely too many patent hassles attached. Microsoft chose to provide poor support for it :) There's just no point in still using it for code like this. Use PNG instead.Hans Passant
If the issue is in the GIF part itself, then here's a Possible Duplicate. Bonus: A direct link to Hans Passant's answertheB
Thanks have seen that, but that would only fix the image when reloading in C#, I effectively need a 'standards compliant' output from GDI+ so I can view the GIF in another program with transparancy or confirmation GDI+ cannot do this. The 'fix' I need would be on the image save, not the image load.SteevieP

1 Answers

0
votes

I suggest you take a look at this Image processing Library ImageMagick http://www.imagemagick.org/

Then use this command to convert the saved gif to a gif with transparent background

convert orig.gif -transparent black transp.gif

This might not produce perfect results but worth a try.You might also want to take a look at ImageMagick's .NET Wrapper https://magick.codeplex.com/ ,if you dont want to do this by running a console command.