Documents for Imaging .NET Edition
Features / Work with GIF files
In This Topic
    Work with GIF files
    In This Topic

    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, GcImaging 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 GcImaging. 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

    Reading Frames from a GIF File

    GcImaging provides GcGifReader 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 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 method of GcWicBitmap and save it as an image using the SaveAsJpeg method of GcBitmap class.  
      C#
      Copy Code
      //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");
          }
      }
      

    Back to Top

    Creating a GIF File

    The GcImaging library provides GcGifWriter class which helps you to create a GIF file using multiple images. The AppendFrame 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 method of GcGifWriter class to append frames to the GIF file.
      C#
      Copy Code
      //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);
      

    Back to Top

    For more information about working with GIF images using GcImaging, see GcImaging sample browser.