Loading and Saving Animated GIFs

The GIF file format is the most wide-spread graphic format that supports animation. Animated images saved in this format can be rendered by almost any modern image viewer. Graphics Mill allows working with GIF images as a whole or with each image frame separately. There are two main classes to deal with GIF files: Codecs.GifReader and Codecs.GifWriter. For more information about format readers and writers see the Working with Files topic.

This topic contains the following sections:

  • reading GIF images, which describes how to load a GIF image and save its frames to separate image files
  • writing GIF images, which demonstrates how to create a GIF image from a set of image files, and how to specify general GIF settings
  • resizing GIF images, which examines how to shrink a GIF image

Reading GIF Images

You can read a GIF image from a file or a stream by using the appropriate GifReader constructor. After the image is opened use the GifReader.Frames property to get access to an image frame or to iterate through all frames in the image.

The following code reads a GIF image and saves its frames as separate PNG files:

C#
using (var reader = new Aurigma.GraphicsMill.Codecs.GifReader(@"Images\slideshow.gif"))
{
    for (int i = 0; i < reader.Frames.Count; i++)
    {
        reader.Frames[i].GetBitmap().Save(@"Images\Output\GIF_frame_" + i.ToString() + ".png");
    }
}

Writing GIF Images

To write a GIF image to a file or a stream use the appropriate GifWriter constructor. The GifWriter properties allows specifying the general GIF image settings, such as playback count, image optimization, etc.

Any bitmap having GIF-supported pixel format can be a frame of a GIF image. You can check if the pixel format is supported either by using the table or via the IsPixelFormatSupported(PixelFormat) method. To add a frame to the writer, run a pipeline by calling the Pipeline.Run method. If you are not familiar with pipelines, read the Understanding Image Processing Approaches in Graphics Mill topic.

The following code creates an animated GIF image from separate PNG files:

C#
int framesCount = 4;

using (var writer = new Aurigma.GraphicsMill.Codecs.GifWriter(@"Images\slideshow.gif"))
{
    writer.FrameOptions.Delay = 100;

    for (int i = 0; i < framesCount; i++)
    {
        using (var reader = Aurigma.GraphicsMill.Codecs.ImageReader.Create(@"Images\GIF_frame_" + i.ToString() + ".png"))
        {
            Aurigma.GraphicsMill.Pipeline.Run(reader + writer);
        }
    }
}

Resizing GIF Images

To resize an animated GIF file you should resize each of its frames separately. So you need to perform the following steps:

  1. Copy general setting of the GIF file.
  2. Iterate through all frames of the image. For each frame perform the following steps:
    1. Copy frame settings to a new frame using the GifWriter.FrameOptions property.
    2. Get the bitmap of the frame.
    3. Preserve the original palette and pixel format.
    4. Resize the bitmap in a high quality mode to avoid color and transparency loss.
    5. Convert the bitmap back to the indexed format keeping the transparency.
    6. Add the frame to the writer by running a pipeline. If you are not familiar with pipelines, read the Understanding Image Processing Approaches in Graphics Mill topic.

The following code resizes an input GIF image to half of original width and height.

C#
using (var reader = new Aurigma.GraphicsMill.Codecs.GifReader(@"Images\slideshow.gif"))
using (var writer = new Aurigma.GraphicsMill.Codecs.GifWriter(@"Images\Output\slideshow1.gif"))
{
    //Copy general properties of the source file
    writer.BackgroundIndex = reader.BackgroundEntryIndex;
    writer.Palette = reader.Palette;
    writer.PlaybackCount = reader.PlaybackCount;

    for (int i = 0; i < reader.Frames.Count; i++)
    {
        //Read a frame
        using (var frame = (Aurigma.GraphicsMill.Codecs.GifFrame)reader.Frames[i])
        using (var bitmap = frame.GetBitmap())
        {
            //Preserve the original palette
            Aurigma.GraphicsMill.ColorPalette palette = bitmap.Palette;
            //Preserve the original pixel format
            Aurigma.GraphicsMill.PixelFormat pixelFormat = bitmap.PixelFormat;

            //Convert the bitmap to a non-indexed format
            bitmap.ColorManagement.Convert(Aurigma.GraphicsMill.ColorSpace.Rgb, true, false);

            //Resize the bitmap in a high quality mode
            bitmap.Transforms.Resize(frame.Width / 2, frame.Height / 2,
                Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode.High);

            //Return to the indexed format
            bitmap.Palette = palette;
            bitmap.ColorManagement.Convert(pixelFormat);

            //Copy frame delay
            writer.FrameOptions.Delay = frame.Delay;

            //Add the frame
            Aurigma.GraphicsMill.Pipeline.Run(bitmap + writer);
        }
    }
}

See Also

Reference

Manual