# Work with GIF files

Use DsImaging to work with GIFs to create animated graphics. Learn how to create a GIF using multiple images or read frames from GIF as separate images. Learn more in DsImaging docs.

## Content

Graphic Interchange Format (GIF) is a commonly used web image format to create animated graphics. GIF file is created by combining multiple images into a single file. Unlike the JPEG image format, GIF file uses lossless data compression technique to reduce the file size without degrading the visual quality.The image data in a GIF file is stored using indexed color which implies that a standard GIF image can include a maximum of 256 colors. 
Apart from reading and creating a GIF file, DsImaging provides control over various features of GIF files. It allows you to set comments for a GIF file. The comment string can be encoded in various formats supported by DsImaging. While creating a multiframed GIF file by appending frames, you can use either an indexed image, bitmap, bilevel bitmap or a grayscale image. It also lets you set the number of iterations that should be executed by the animated GIF file.
The below image represents the creation of a GIF file using different frames and the extraction of different frames as images while reading a GIF file.
![GIF of a radially moving arc of light and its different frames](https://cdn.mescius.io/document-site-files/images/a5680f0e-2d32-45dc-8048-d3ef11d0dae7/images/gif.png?width=570)![](https://cdn.mescius.io/document-site-files/images/a5680f0e-2d32-45dc-8048-d3ef11d0dae7/images/new_radar.gif?width=420)

## Reading Frames from a GIF File

DsImaging provides [GcGifReader](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcGifReader.html) class that helps you to read a GIF file and save the frames as separate images. The constructor of this class accepts the GIF file name or stream as a parameter and loads the contents of GIF file. The information about the individual GIF frames is collected in the [Frames](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcGifReader.Frames.html) property of the GcGifReader class. While extracting the frames, you can process them in a number of ways, store them in different formats or add them as input frames to GcGifWriter to create a GIF. 
To read a multiframe GIF file and save its frames as separate images:

1. Initialize the **GcGifReader** class and pass the GIF file name as a parameter to the constructor.
2. Access the GIF frames from the GIF file using **Frames** property of the GcGifReader class.
3. Load the frame using [ToGcBitmap](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging.Windows/GrapeCity.Documents.Imaging.Windows.GcWicBitmap.ToGcBitmap.html) method of GcWicBitmap and save it as an image using the [SaveAsJpeg](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcBitmap.SaveAsJpeg.html) method of GcBitmap class.

    ```csharp
    //Read frames form the GIF image
    GcGifReader reader = new GcGifReader("Images/radar.gif");
    var frames = reader.Frames;
    
    using (var bmp = new GcBitmap())
    {
        //Saving GIF frames as individual images
        for (var i = 0; i < frames.Count; i++)
        {
            frames[i].ToGcBitmap(bmp, i - 1);
            bmp.SaveAsJpeg("Images/Frames/Radar/fr" + (i + 1).ToString() + ".jpg");
        }
    }
    ```

## Creating a GIF File

The DsImaging library provides [GcGifWriter](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcGifWriter.html) class which helps you to create a GIF file using multiple images. The [AppendFrame](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcGifWriter.AppendFrame.html) method of **GcGifWriter** class appends an image as a frame to the GIF file. You can invoke this method multiple time to append multiple frames and create a GIF file.
To create a GIF file using multiple images:

1. Initialize the **GcGifWriter** class and pass the GIF file name as a parameter to the constructor.
2. Instantiate **GcBitmap** class to load the images which will serve as frames for the multiframe GIF file.
3. Invoke the [AppendFrame](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcGifWriter.AppendFrame.html) method of GcGifWriter class to append frames to the GIF file.

    ```csharp
    //Creating GIF image using set of images            
    GcGifWriter writer = new GcGifWriter("Images/newradar.gif");            
    
    GcBitmap bmp = new GcBitmap();
    bmp.Load("Images/Frames/fr1.jpg");
    writer.AppendFrame(bmp,255,0,0,GifDisposalMethod.DoNotDispose,20,false);
    
    bmp.Load("Images/Frames/fr2.jpg");
    writer.AppendFrame(bmp, 255, 0, 0, GifDisposalMethod.DoNotDispose, 20, false);
    
    bmp.Load("Images/Frames/fr3.jpg");
    writer.AppendFrame(bmp, 255, 0, 0, GifDisposalMethod.DoNotDispose, 20, false);
    
    bmp.Load("Images/Frames/fr4.jpg");
    writer.AppendFrame(bmp, 255, 0, 0, GifDisposalMethod.DoNotDispose, 20, false);
    ```

For more information about working with GIF images using DsImaging, see [DsImaging sample browser](https://developer.mescius.com/documents-api-imaging/demos/basics/gif/make-gif/code-cs).