Note

I have donated TGIFImage to CodeGear and do not support it anymore.

Read more below.

TGIFImage is a full TGraphic implementation of the GIF graphics format.

It reads, writes and displays animated GIFs and can convert to and from TBitmap (i.e. the BMP format) and any other TGraphic derived format (e.g TJPEGImage, TIcon and TMetaFile).TGIFImage can either be used stand-alone or as part of Delphi’s extensible TImage/TPicture/TGraphic framework. When used in connection with the TImage component, TGIFImage registers itself with the TPicture class, giving you instant support for the GIF format throughout Delphi. After installing TGIFImage, you will be able to load, save and display GIFs without any additional code!

TGIFImage has full support for animated GIF and is the only solution that can display animated GIFs in the TImage component.

TGIFImage implements the complete GIF87a and GIF89a specification as well as all common GIF extensions:

  • Transparency and interlacing
  • Local- and global palettes of 2,4,8,16,32,64,128 & 256 colors
  • Palette optimization
  • Delay and Loops for animations
  • Text- and comment extension
  • Application extensions
  • Disposal to previous frame and background image
  • And much more…

Support for new Application Extension types can be added by the developer without modification to the TGIFImage source by registering the format at run-time with TGIFImage.

GIF Explorer Demo Application

To demonstrate its use, TGIFImage comes with several sample applications: The “GIF Explorer” that can be used to view GIF files and examine their structure, a “simple” web-counter that demonstrates how to use TGIFImage with the Delphi CGI, ISAPI and NSAPI components together, an animated button component and an AVI to GIF and GIF to AVI converter just to mention a few of them.

Example 1

Converting a BMP file to a GIF file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
procedure ConvertBMP2GIF;
var
  bmp: TBitmap;
  gif: TGIFImage;
begin
  gif := TGifImage.Create;
  try
    bmp := TBitmap.Create;
    try
      bmp.LoadFromFile('MyBitmap.BMP');
      gif.Assign(bmp);
    finally
      bmp.Free;
    end;
    gif.SaveToFile('MyBitmap.GIF');
  finally
    gif.Free;
  end;
end;

Many other freeware GIF libraries exists, so you might wonder why we need another one… The need for TGIFImage arose when I was writing a CGI application that needed to create large GIF images on-the-fly. After having tested a number of freeware libraries I concluded that they weren’t quite fit for the job with regard to performance and GIF compliance. Some of the DLL based implementations were OK, but Delphi integration were missing. So after reading through the GIF specifications and several articles on LZW compression, I decided that it couldn’t be to hard to whip something useful together.

The name TGIFImage might be a bit unfortunate as almost every known Delphi implementation of the GIF format is called TGIFImage, but it’s the most logical name for the gizmo and it shouldn’t be a problem once I have made the “competing” implementations obsolete :-)

TGIFImage has been tested with several thousand GIF files, so I’m fairly confident that it will handle whatever you throw at it. If you find a valid GIF file that cannot be handled correctly by TGIFImage, please mail it to me with a description of the problem enter a QC report, and I’ll give you your money back :-)

So what can TGIFImage do and how does it differentiate itself from some of the other implementations?

Features

  • Full source included.
  • Derived from TGraphic.
  • Supports all palette sizes.
  • Fast decode and encode.
  • Based on known solid algorithms.
  • Handles unknown extensions gracefully.
  • Tolerant. Loads and displays corrupt GIFs.
  • Easy to extend.
  • Reduces file size by minimizing palette size.
  • Does not use idle-looping.
  • Integrates with TImage, TOpenPictureDialog and TSavePictureDialog.
  • Dithers to the Netscape web-safe palette on 256 color systems.
  • Displays animations in TImage.
  • Non-destructive GIF optimizer reduces GIF file size.
  • Context sensitive on-line help.
  • Automatic color reduction of bitmaps with more than 256 colors using color quantization and 6 different dithering methods (e.g. Floyd Steinberg).
  • Supports LZW-less compression using GIF compatible RLE encoding. This makes it possible to create GIFs without paying royalties to Unisys!

Current Status

In February 2007 I donated TGIFImage to CodeGear with the purpose of adding in-the-box GIF support to the VCL, starting with Delphi 2007.

Besides giving me motivation to work on TGIFImage again, the transfer of ownership also gave me an opportunity to pull TGIFImage out the limbo it had been in and secure future development.

Where to get TGIFImage?

The official TGIFImage version 2.2 release is available from a lot of places on the net. However since the official release hasn’t been maintained since I distributed it in May 1999, I strongly suggest you use one of the following releases instead.

Finn Tolderlund’s Version 2.2.*

Finn has been maintaining TGIFImage on his own since my former web site disappeared. His version is based on the official TGIFImage version 2.2 release. For an updated TGIFImage version 2.2, supporting Delphi 5-2007, please go to Finn Tolderlund’s web site. In addition to the updated sources, I recommend you also get the original version 2.2 installation kit (also available from Finn’s site), since that contains the documentation and all the demos.

CodeGear’s Version 3.0

Since Delphi 2007 TGIFImage version 3.0 is included with all revisions of Delphi. Please note that this version does not support earlier versions of Delphi.

Version 3.0 was written by me and is a fork of version 2.3, a version that was never widely released. By fork I mean that it isn’t meant as a progressive continuation of the old TGIFImage. Backward compatibility was not a design requirement of the new TGIFImage, so I took the opportunity to remedy some of the things that I felt was wrong in the old version and some of these things could only be done without consideration for backward compatibility. Those that need backward compatibility should use Finn’s version.

Version 3 differs from version 2.2 in the following ways:

  • The interface has changed, but it is drop-in compatible with earlier version if one doesn’t use the TGIFImage class directly.
    E.g. if you just use TImage to display GIFs, then you should be fine.
  • Many classes and properties have been renamed for better consistency.
    E.g. TGIFSubImage is now named TGIFFrame.
  • The rendering engine has been completely rewritten from scratch.
    The rendering of animated GIFs is now handled with a timer and windows messages instead of a thread. The primary implication of this change is that the rendering is now done synchronously instead of asynchronously. Not as sexy but more safe.
  • Animation is disabled by default.
    This was done for consistency with other (actually all other) TGraphic class (e.g. TBitmap, TIcon, etc). I judged that the average user wouldn’t want (or expect) animated content in TImage as no other TGraphic descendants support it. Please see the FAQ for a description of how to enable animation.
  • Support for earlier versions of Delphi has been removed.
    Version 3 has only been tested on Delphi 2007 and later; It may or may not work with Delphi 2005 or Delphi 2006.
  • The tiling feature has been removed from the TImage rendering.
    The low level rendering engine still supports it though.
  • It is no longer necessary to edit the source to configure options.
    Most of the options that were previously configurable with conditional defines (i.e. compile time options) are now controlled with variables (i.e. run time options).

Since CodeGear didn’t include any of the demos I wrote for version 3.0, you might want to grab a copy of the version 2.2 installation kit too. Although the demos won’t run with version 3.0 and most of the documentation is obsolete, it’s better than nothing. If time permits, I will try to post some of the demos that I have ported to version 3 here.

[Update: 03-apr-2008] The first of the demos is now available: Animation Demo for TGIFImage v3.

Support

I do not provide unpaid support for TGIFImage anymore. Sorry!

Version 2.x

For assistance with TGIFImage version 2.x, I suggest you seek help in the embarcadero.public.delphi.thirdpartytools.general newsgroup.

Bugs in Finn’s version of TGIFImage should be reported to Finn.

Version 3.x

For assistance with TGIFImage version 3.x, I suggest you try the embarcadero.public.delphi.graphics newsgroup or open a support case with CodeGear.

Bugs should be reported in Quality Central.

FAQ - Frequently Asked Questions

  1. Where’s the gifimage unit in Delphi 2007 (and later)?
  2. I have made a small application that displays a GIF in a TImage component. The GIF displays fine in design mode in Delphi but nothings shows up when the application is running. What’s wrong?
  3. How do I make a transparent GIF?
  4. Hmmm. Isn’t there an easier way to make a transparent GIF?
  5. My GIFs looks strange in [some application], but displays correctly in other applications. Some of my old applications can’t load GIFs created by TGIFImage. Java application fails to display GIFs produced by TGIFImage with an “sun.awt.image.ImageFormatException: no global colormap in GIF file” error.
  6. I have created an animated GIF but I can’t get it to loop. What’s wrong?
  7. When I load a form with a TImage that contains a GIF I get a an error message saying something like “Out of memory while expanding stream”. I have plenty of memory in my system. What’s wrong?
  8. What do I need to do in order to use GIFs with the TImage component?
  9. I have TGIFImage installed but I can’t find it anywhere on the component palette. Where is it?
  10. I have converted a bitmap to a GIF using the TGIFImage.Assign method but the colors of the GIF are all wrong. Is this a bug?
  11. How do I change the color used for transparency?
  12. I receive a “Color table overflow” exception when I set the BackgroundColor property.
  13. Is it true that the GIFs format supports a maximum of only 256 colors?
  14. When I load an animated GIF into Timage in Delphi it displays the GIF but does not animate.
  15. What is the difference between the ColorMap and ActiveColorMap properties?
  • Positive  Negative
    Rating: +2121

    Where’s the gifimage unit in Delphi 2007 (and later)?

    In Delphi 2007+ (i.e. TGIFImage version 3) the gifimage unit has been split into two units, GIFImg and GIFConsts, located in the RAD Studio/x.0/source/TGIFImage folder.
    This was done to avoid conflicts with older versions of TGIFImage, some which are still bundled with many 3rd party libraries.

  • Positive  Negative
    Rating: +1

    I have made a small application that displays a GIF in a TImage component. The GIF displays fine in design mode in Delphi but nothings shows up when the application is running. What’s wrong?

    You have probably forgotten to include the GIFImg unit in your uses clause. Unless you add GIFImg to one of the uses clauses in your application (doesn’t matter where), there is no way your application can know what to do with a GIF.

  • Positive  Negative
    Rating: +2

    How do I make a transparent GIF?

    Try something like this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    var
      GIF: TGIFImage;
      Ext: TGIFGraphicControlExtension;
    begin
      GIF := TGIFImage.Create;
      try
        // Convert bitmap to GIF
        GIF.Assign(Bitmap);
     
        // Create an extension to set the transparency flag
        Ext := TGIFGraphicControlExtension.Create(GIF.Images[0]);
        Ext.Transparent := True;
     
        // Set transparent color to lower left pixel color
        Ext.TransparentColorIndex := GIF.Images[0].Pixels[0, GIF.Height-1];
     
        // Do something with the GIF here...
        ...
      finally
        GIF.Free;
      end;
    end;

    You should also check out the Animate demo application for an example of this.

  • Positive  Negative
    Rating: +6

    Hmmm. Isn’t there an easier way to make a transparent GIF?

    Yes of course there is:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    var
      GIF: TGIFImage;
    begin
      GIF := TGIFImage.Create;
      try
        // Make bitmap transparent
        Bitmap.Transparent := True;
     
        // Convert bitmap to GIF
        GIF.Assign(Bitmap);
     
        // Do something with the GIF here...
        ...
      finally
        GIF.Free;
      end;
    end;

    The lesson here is that TGIFImage will automatically convert a transparent bitmap (TBitmap.Transparent = True) to a transparent GIF.

  • Positive  Negative
    Rating: +1

    My GIFs looks strange in [some application], but displays correctly in other applications. Some of my old applications can’t load GIFs created by TGIFImage. Java application fails to display GIFs produced by TGIFImage with an “sun.awt.image.ImageFormatException: no global colormap in GIF file” error.

    PhotoShop, the Java GIF library and some older DOS applications (e.g. VPIC) is unable to handle local color maps (common in animated GIFs) and only support GIFs with global color maps.
    To work around this problem, you can use the TGIFImage.OptimizeColorMap method to convert all local color maps to a single global color map.
    Generally, if there is a difference between how a GIF looks in a browser and some other application, the browser has it right. TGIFImage has been designed to display GIFs like most browsers do.

  • Positive  Negative
    Rating: +5

    I have created an animated GIF but I can’t get it to loop. What’s wrong?

    You need to add a “Netscape Loop” extension block to the first frame of your GIF.
    The loop block must be the first extension you define for the frame or else it will not work.

    See the Animate demo for an example of how to build an animated GIF.

  • Positive  Negative
    Rating: -1

    When I load a form with a TImage that contains a GIF I get a an error message saying something like “Out of memory while expanding stream”. I have plenty of memory in my system. What’s wrong?

    You probably have another GIF library installed.
    The error is often caused by one GIF library attempting to load an image stored in a form (or some other stream) by another, incompatible, GIF library. The RxLib (now part of Jedi) GIF library is known to store GIF data in a format that is incompatible with most other GIF libraries.

  • Positive  Negative
    Rating: +2

    What do I need to do in order to use GIFs with the TImage component?

    The only thing you need to do, is to add the GIFImg unit to a uses clause anywhere in your project:

    Uses
      GIFImg, Windows, Messages, SysUtils, Classes... etc.

    When you do this, TGIFImage will automagically register itself with the TPicture class in the initialization section of the GIFImg unit.
    TPicture is the class that TImage uses to support different graphic formats.

  • Positive  Negative
    Rating: +4

    I have TGIFImage installed but I can’t find it anywhere on the component palette. Where is it?

    TGIFImage isn’t a component and as such won’t show up on the component palette. Only classes that are derived from TComponent are real components. I often refer to TGIFImage as a component, but this is only because it’s easier for people to understand.

    TGIFImage is just like TBitmap, TStringList, TIniFile etc; You use it in your code, not on a form.
    If you need to display a GIF on a form, just use the TImage component, but remember to reference the GIFImg unit or the TGIFImage library won’t be linked into your application.

  • Positive  Negative
    Rating: +1

    I have converted a bitmap to a GIF using the TGIFImage.Assign method but the colors of the GIF are all wrong. Is this a bug?

    No, probably not.
    More likely the problem is that your bitmap is in a format that can potentially contain much more than 256 colors.

    In order to be able to import bitmaps with more than 256 colors, TGIFimage first reduces the potential number of colors to a maximum of 256, and then converts the bitmap to 8 bit format. This is necessary because the GIF format doesn’t allow more than 256 colors.
    You can control the method TGIFImage uses to choose the colors that are used in the final GIF, by setting the GIFImageDefaultColorReduction variable.
    The default color reduction mode is rmNetscape (fixed 216 color “web-safe” palette) because it is extremely fast. This means that even if your bitmap contains 256 colors or less, it will be reduced to the 216 color Netscape palette. To avoid this, you must use one of the more intelligent color reduction methods (such as rmQuantize).

  • Positive  Negative
    Rating: +1

    How do I change the color used for transparency?

    The following code snippet changes the transparency color of all frames to white:

    for i := 0 to GIF.Images.Count-1 do
      with GIF.Images[i] do
        if (Transparent) then
          ActiveColorMap[GraphicControlExtension.TransparentColorIndex] := clWhite;
  • Positive  Negative
    Rating: -2

    I receive a “Color table overflow” exception when I set the BackgroundColor property.

    This happens when the GIF already contains 256 colors and the color your are specifying isn’t one of these.

    Internally a GIF stores the background color as an index into the color map (the BackgroundColorIndex property). If the new background color already exists in the color map, TGIFImage just uses that color, otherwise TGIFImage must add it to the color map. If the color map is already full you get the above error.

  • Positive  Negative
    Rating: -2

    Is it true that the GIFs format supports a maximum of only 256 colors?

    Yes and no…

    A single GIF frame can only contain up to 256 colors, so for a non-animated GIF the answer is Yes. However, since there’s no limit on the number of frames you can have in a GIF, the more correct answer is actually No; It is possible to construct GIF images with any number of colors.

    When I get a spare moment (yeah right), I’ll see if I can make a small program that demonstrates the different methods of creating true color GIFs.

  • Positive  Negative
    Rating: +14

    When I load an animated GIF into Timage in Delphi it displays the GIF but does not animate.

    As you’ve discovered I chose to disable animation by default. The primary cause was that I judged that the average user wouldn’t want (or expect) animated content in TImage as no other TGraphic descendants support it.

    Anyhow, to enable GIF animation globally just set the GIFImageDefaultAnimate global variable to True:

      GIFImageDefaultAnimate := True;

    and then load the GIF into your TImage as usual.

    You can do the same for transparency with the GIFImageDefaultTransparent global variable:

      GIFImageDefaultTransparent := True;

    If your GIF is both transparent and animated you should double buffer the parent of the TImage control to avoid flickering. E.g. by placing the TImage on a TPanel and setting the panel’s DoubleBuffered property to True.

  • Positive  Negative
    Rating: +1

    What is the difference between the ColorMap and ActiveColorMap properties?

    A color map is the GIF terminology for a palette.
    A GIF frame can either use a local or a global color map. A local color map only exists for the frame that defines it while the global color map is shared among all frames.
    The ColorMap property contains the local color map.
    The ActiveColorMap property points to the color map that the frame actually uses.
    If the local color map is empty and the global color map is not empty, then ActiveColorMap will point to the global color map. Otherwise ActiveColorMap points to the local color map.