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.
DsImaging 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:
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"); } } |
The DsImaging 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:
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); |
For more information about working with GIF images using DsImaging, see DsImaging sample browser.