1

I've been playing around with NGif Animator for resizing animated gifs and it does resize but parts in many animated gifs I've tried get erased. I looked through the comments on that page and didn't see anyone else mention it.

To eliminate resizing as the cause I simply loop through the frames and save each one. Each frame is a System.Drawing.Image. Transparency is set to none (Color.Empty).

This is my test method currently:

        GifDecoder gifDecoder = new GifDecoder();
        MemoryStream memoryStream = new MemoryStream();
        new BinaryWriter((Stream)memoryStream).Write(imageToResize); // byte array
        memoryStream.Position = 0L;

        gifDecoder.Read((Stream)memoryStream);
        memoryStream.Dispose();

        string filename = Guid.NewGuid().ToString().Replace("-", String.Empty) + ".gif";
        string output = path + @"\" + filename;

        AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder();
        animatedGifEncoder.Start(output);
        animatedGifEncoder.SetRepeat(gifDecoder.GetLoopCount());
        animatedGifEncoder.SetQuality(10); // They say 20 is max quality will get, I've tried higher.  Makes it a little bit better but black areas instead of gray. 10 is their default.
        animatedGifEncoder.SetTransparent(Color.Empty); // This is default either way

        int frameCount = gifDecoder.GetFrameCount();
        int num = 0;

        Image frame;
        Image image = null;

        for (int index = frameCount; num < index; ++num)
        {
            frame = gifDecoder.GetFrame(num);
            animatedGifEncoder.SetDelay(gifDecoder.GetDelay(num));

            string fname = @"C:\Development\images\frame_" + num.ToString() + ".gif";
            if (File.Exists(fname)) { File.Delete(fname); }
            frame.Save(fname);

            animatedGifEncoder.AddFrame(image);
        }

        animatedGifEncoder.Finish();

Here's an example of what's happening:

Background is gone

The background is gone and it's gray.

It's supposed to look like:

Good background

Anyone have experience with NGif and know what would cause this? The first frame is always fine. It's the others after that have a problem so I'm guessing something isn't being reset from frame to frame (or re-read). I've been adding more things to their reset frame method but so far it hasn't helped. That now looks like:

    protected void ResetFrame() 
    {
        lastDispose = dispose;
        lastRect = new Rectangle(ix, iy, iw, ih);
        lastImage = image;
        lastBgColor = bgColor;

        delay = 0;
        transparency = false; // I don't want transparency
        lct = null;
        act = null;
        transIndex = -1;
    }
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
GregInWI2
  • 904
  • 1
  • 17
  • 26

1 Answers1

0

There is actually a bug in their code, a byte array not being reset. Check the comments on their page for a solution

Gilthans
  • 1,656
  • 1
  • 18
  • 23