none
editing animated gif's in c#

    Question

  • Is there a way to edit an animated gif in .net? I need to load up an animated gif image, then give the user the ability to crop it, and then save the cropped image. It seems that I would need to isolate each frame from the image (how?), crop the frame, and then combine them somehow into a new gif file. Is this possible, or is there an easier way? If we can isolate each frame as a bitmap image, and then resave them into a gif, will their be quality loss? Thanks.

    Wednesday, September 05, 2007 9:52 AM

Answers

  • You can read the animated Gif with Image.GetFrameCount() and SelectActiveFrame().  You can even get the frame durations with Image.GetPropertyItem(0x5100).  But that's where the buck stops.  You can't save it back, the GDI+ Gif encoder doesn't support the Image.SaveAdd() method.  You'll have to turn to a more advanced image processing library like LeadTools or ImageMagick.

    Here's GDI+ code to do the reading:

    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Collections.Generic;
    using System.IO;

    public class AnimatedGif {
      private List<AnimatedGifFrame> mImages = new List<AnimatedGifFrame>();
      PropertyItem mTimes;
      public AnimatedGif(string path) {
        Image img = Image.FromFile(path);
        int frames = img.GetFrameCount(FrameDimension.Time);
        if (frames <= 1) throw new ArgumentException("Image not animated");
        byte[] times = img.GetPropertyItem(0x5100).Value;
        int frame = 0;
        for (; ; ) {
          int dur = BitConverter.ToInt32(times, 4 * frame);
          mImages.Add(new AnimatedGifFrame(new Bitmap(img), dur));
          if (++frame >= frames) break;
          img.SelectActiveFrame(FrameDimension.Time, frame);
        }
        img.Dispose();
      }
      public List<AnimatedGifFrame> Images { get { return mImages; } }
    }

    public class AnimatedGifFrame {
      private int mDuration;
      private Image mImage;
      internal AnimatedGifFrame(Image img, int duration) {
        mImage = img; mDuration = duration;
      }
      public Image Image { get { return mImage; } }
      public int Duration { get { return mDuration; } }
    }
    Wednesday, September 05, 2007 1:34 PM

All replies

  • You can read the animated Gif with Image.GetFrameCount() and SelectActiveFrame().  You can even get the frame durations with Image.GetPropertyItem(0x5100).  But that's where the buck stops.  You can't save it back, the GDI+ Gif encoder doesn't support the Image.SaveAdd() method.  You'll have to turn to a more advanced image processing library like LeadTools or ImageMagick.

    Here's GDI+ code to do the reading:

    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Collections.Generic;
    using System.IO;

    public class AnimatedGif {
      private List<AnimatedGifFrame> mImages = new List<AnimatedGifFrame>();
      PropertyItem mTimes;
      public AnimatedGif(string path) {
        Image img = Image.FromFile(path);
        int frames = img.GetFrameCount(FrameDimension.Time);
        if (frames <= 1) throw new ArgumentException("Image not animated");
        byte[] times = img.GetPropertyItem(0x5100).Value;
        int frame = 0;
        for (; ; ) {
          int dur = BitConverter.ToInt32(times, 4 * frame);
          mImages.Add(new AnimatedGifFrame(new Bitmap(img), dur));
          if (++frame >= frames) break;
          img.SelectActiveFrame(FrameDimension.Time, frame);
        }
        img.Dispose();
      }
      public List<AnimatedGifFrame> Images { get { return mImages; } }
    }

    public class AnimatedGifFrame {
      private int mDuration;
      private Image mImage;
      internal AnimatedGifFrame(Image img, int duration) {
        mImage = img; mDuration = duration;
      }
      public Image Image { get { return mImage; } }
      public int Duration { get { return mDuration; } }
    }
    Wednesday, September 05, 2007 1:34 PM
  • thanks.

     

    Wednesday, September 05, 2007 1:47 PM
  •  

    Hi, I'm working on something similar.

     

    MSDN says:

     

    Image.SaveAdd Method:

    Adds a frame to the file or stream specified in a previous call to the Save method. Use this method to save selected frames from a multiple-frame image to another multiple-frame image.

     

    Doesn't that mean that you can add another frame to an image, or am I misunderstanding something here?

     

    If it's not possible to use Image.SaveAdd to create a gif image then what is the method good for?

    Tuesday, March 18, 2008 9:01 PM
  • Image.SaveAdd it's for multi page tiff images.
    Thursday, April 16, 2009 1:22 PM