GifComponents and GifInspector
GifInspector
GifInspector is a simple Windows Forms application which uses the GifDecoder class in the GifComponents library to decode a GIF file and display all its component parts. It also allows the user to extract and save the images from the individual frames in the GIF file.
I wrote this application mainly to help me test and troubleshoot the GifComponents library. Other than that it doesn't do anything particularly useful besides allowing you to look at the internals of a GIF file. I've included it in this project mainly as an example of how the GifDecoder can be used.
GifComponents
For most uses of GifComponents, there are only three classes you need to know about; GifFrame, GifDecoder and AnimatedGifEncoder.
GifFrame class
The GifFrame is the basic building block of a GIF file. Every GIF file contains one or more frames. To instantiate a GifFrame, you need a System.Drawing.Bitmap object. This must be passed to the constructor along with the number of hundredths of a second that the frame should be displayed before the next frame in the animation is displayed. For example:
Bitmap myBitmap = Bitmap.FromFile( "myBitmap.bmp" );
GifFrame myFrame = new GifFrame( myBitmap, 100 );
The above example will create a frame containing the image myBitmap.bmp which will display for 1 second when added to a GIF file.
GifDecoder class
The GifDecoder reads in a System.IO.File or a System.IO.Stream containing a GIF file or data stream. Once instantiated, it exposes a variety of properties which allow the user to interrogate the contents of the file - see the
developer documentation for more information about these properties. For example:
GifDecoder myDecoder = new GifDecoder( "myGIF.gif" );
int i = 0;
foreach( GifFrame thisFrame in myDecoder.Frames )
{
Bitmap thisImage = thisFrame.TheImage;
Console.WriteLine( "frame " + i + " has a delay of " + thisFrame.Delay + "/100 seconds" );
thisImage.Save( "frame" + i + ".bmp" );
i++;
}
The above example will save a series of bitmap files containing the images held in each frame of an animated GIF file, and will write the delay time of each frame to the console.
AnimatedGifEncoder class
The AnimatedGifEncoder creates an animated GIF file or stream made up of any GifFrames which are passed to it. It can produce a file with a single Global Colour Table for all frames, or a local colour table for each frame in the file. For example:
Size screenSize = new Size( 10, 10 ); // size of final image
int repeatCount = 10; // repeat animation 10 times
ColourTableStrategy strategy = ColourTableStrategy.UseGlobal; // no local colour tables
int quality = 10; // used when there are more than 256 colours in the pallette
AnimatedGifEncoder myEncoder = new AnimatedGifEncoder( screenSize, repeatCount, strategy, quality );
Bitmap image1 = Bitmap.FromFile( "image1.bmp" );
Bitmap image2 = Bitmap.FromFile( "image2.bmp" );
myEncoder.AddFrame( image1, 100 );
myEncoder.AddFrame( image2, 1 );
myEncoder.WriteToFile( "myAnimation.gif" );
The above example will create a GIF file called myAnimation.gif with a single global colour table and two frames. The first frame will be displayed for 1 second and the second will be displayed for one hundredth of a second. The animation will repeat 10 times before stopping.
Three other versions of the AddFrame method are available, which provide the option of setting a flag to indicate whether the rendering device should wait for user input before displaying the next frame, and of specifying a point within the logical screen which marks the top-left corner of the frame, if it differs from the top-left corner of the logical screen.
A WriteToStream method is also provided, allowing the encoded GIF data to be written to a System.IO.Stream rather than a file.
Licenses
The GifComponents library is released under the
Code Project Open License as this is the license that the original work was released under.
Both of these are open source licenses, so you are free to download the source code, tinker with it to your heart's content, incorporate it into your own projects and share them with the world, provided you stick to the terms of the relevant licenses.
Download
Other than the .net runtime, if you download and run the
Windows Installer package that will install everything you need to run the software, including adding the GifComponents library to the Global Assembly Cache.
If you download the
source code then you will need
Nunit.Extensions too in order to run the unit tests, either by downloading the code or the Windows Installer package from that project's page or by running the GifInspector Windows Installer package (which also adds NUnit.Extensions to the Global Assembly Cache).