DataDink / Bumpkit

A .NET imaging library that extends System.Drawing functionality
The Unlicense
81 stars 23 forks source link

Transparent Animated GIF #11

Open erviveksoni opened 7 years ago

erviveksoni commented 7 years ago

I have a sequence of PNG images which I want to use for generating an animated gif. The library works well by stitching the png images to make a gif but looses the transparency while conversion.

The converted gif appears to be grey and white.

Any fix for this?

DataDink commented 7 years ago

Hi erviveksoni,

It is unlikely I will get around to this any time soon. If you do find a solution, please submit a pull request and I'll make sure to get it in.

zhaDeth commented 7 years ago

@erviveksoni Here's a fix : I had the same Issue with transforming PNGs to GIF, and found out (after hours of trying with lots of different Gif Encoding libs..) it's not really the BumpKit library misbehaving, it has something to do with how the GIF format handles its palette or something... To be honest I didn't bother with the technical stuff once I had the fix: http://www.nathansokalski.com/code/TransparencyClass.aspx (its in VB but probably not a problem for those VB to C# converter sites)

Basically, with this class in your project, all you have to do is call Transparency.makeTransparent() to obtain a GIF-transparency-friendly copy of each Image/Bitmap frame you add to the GifEncoder:

frames(0) = Image.FromFile("1.png")
frames(1) = Image.FromFile("2.png")
frames(2) = Image.FromFile("3.png")

using (var gif = File.OpenWrite(""))
using (var encoder = new GifEncoder(gif))
    for (var i = 0; i < 2; i ++)
    {
        encoder.AddFrame(Transparency.MakeTransparent(frames(i),color.transparent));
        // you might want to dispose frames(i) here since 'MakeTransparent' creates a new copy
    }

flammie

DataDink commented 7 years ago

That sounds like an ideal solution. I'll try to take a look this weekend.