GifImage FAQ Last updated: 2008-11-08. ----- QUESTIONS ----- Is TGIFImage copyrighted? Can it be used in commercial applications? Did you (Finn Tolderlund) make the GifImage.pas unit? What is the GifImage.pas unit, what does it do? Can you tell me how to install GifImage as a component in delphi 7? How to install GifImage as a component in BDS 2006 How do you use the GifImage unit? How to display animated GIF picture on the form? I know how to load a GIF file from the harddrive using Image1.Picture.LoadFromFile but is it possible to load a GIF file from a resource instead? I used Tgifimage for Delphi 5, but i can't get it working in Delphi 6/7. Can jou tell me how to use Tgifimage in Delphi 6/7? Is it possible to insert 5 or 6 different GIF images in to delphi image component? How do I load a GIF image to a TBitMap? I can't figure out (after spending a long time reading the help and looking at the demo code) how to get the index of the current frame in an animated gif. This would be very useful in order make a text comment in a memo when a certain frame is played thereby synchronizing text and gif. Is there a way to get the frame number currectly displayed? Is it possible to make an animated Gif start at a certain frame? Is there any quick way of setting a property (or maybe introducing a new property), so I can tell TGifImage which frame to display? In this case i want to display just 1 frame of the many frames of a GifImage. If the GifImage is not animating, it only displays the first frame. I want to tell which frame to display. How do I create a GIF with a transparent background? I am working with TGifImage on a win xp system with lots of Ram. (>500) If I use a win98 system with only 128 mb of ram, it crashes in the designer. What is the problem? Does TGifImage work in Kylix on the Linux platform? Does TGifImage work in .NET? What Delphi versions does TGifImage work with? ----- ANSWERS ----- Is TGIFImage copyrighted? Can it be used in commercial applications? The TGIFImage unit are copyrighted, but can be used in commercial applications. Please see the included help file for details. My unit to make animated gif files is free for use as you see fit. ----- Did you (Finn Tolderlund) make the GifImage.pas unit? No, it was originally made by Anders Melander for use with Delphi 1-5. It was available on his website http://www.melander.dk/ but the site has been down for some time now. With Anders permission I modified his unit so that it is compatible with Delphi 6 and Delphi 7, and put it on my website: http://www.tolderlund.eu/ http://home20.inet.tele.dk/tolderlund/delphi/ ----- What is the GifImage.pas unit, what does it do? It is a Delphi unit (containing a TGIFImage class) which you can add to your Delphi program. With this unit you can load and display GIF files using Delphi's standard TImage component, including animated GIF files, In addition to display GIF files: It can be used to create new gif files, including creating animated gif files. You can convert a bitmap to a gif file. You can convert a gif file to a bitmap. The GifImage unit can be downloadet from http://www.tolderlund.eu/delphi/ http://finn.mobilixnet.dk/delphi/ http://home20.inet.tele.dk/tolderlund/delphi/ If you want to see examples how to use the GifImage unit, you should download the GIF demo projects. ----- Can you tell me how to install GifImage as a component in delphi 7? It is not a component, just a Delphi unit. You do not need to install it in Delphi. However, you can add the unit to a design time package if you want. But you only need to do this if you want to load gif files at design time. I normally recommend to put the GifImage.pas file in a directory where you keep other Delphi units you might have with usefull procedures and functions. Add this directory to Delphi's search path in "Tools, Environment Options, Library, Library path". Then you do not need to specify anything in Project Options when you need to use the unit, just add GifImage to your program's uses clause. By doing this you can only load and display GIF images at runtime, not at designtime. If you want to add the unit to a design time package in order to enable you to load gif files at design time in the Delphi IDE, this is the steps you need to follow: 1. Create a directory where you place the GIFImage.pas unit. Example directory: C:\D7Components\GifImage Place the GIFImage.pas unit in this directory. 2. Start Delphi 7 and select the menu items Component, Install Component... 3. Select the "Into new package" tab. 4. In the "Unit file name" box enter the path "C:\D7Components\GifImage\GIFImage.pas". 5. In the "Package file name" box enter a suitable name for the package such as "C:\D7Components\GifImage\GIFImageD7.dpk". (The package name must be different from "GIFImage.dpk") 6. In the "Package description" box enter a description such as "GifImage Design Time Package". 7. Click OK. 8. You will be asked to confirm that the package is built and then installed. 9. Click Yes. Or Click No, close the editor window, click the option button, select the "Designtime only" radiobutton, click OK, Click Compile and OK, Click Install and OK. 10. Click OK to the message that the package has been installed. 11. Select File, Close All. Click Yes to save changes to the package. 12. Go in to Tools, Environments Options, Library tab. Confirm that the directory "C:\D7Components\GifImage" has been added to the "Library path". If it isn't there, add the directory manually. 13. That's all. Note that since TGIFImage is not a component, it will not show in the component palette. If you later want to remove the design time package: 1. Select Component, Install Pakcages... 2. Scroll down and select the "GifImage Design Time Package". 3. Click Remove and OK. 4. Remove the directory path from Tools, Environments Options, Library tab, "Library path" box. 5. Click OK. How to install GifImage as a component in BDS 2006 I normally recommend to put the GifImage.pas file in a directory where you keep other Delphi units you might have with usefull procedures and functions. Add this directory to Delphi's search path in "Tools, Options, Delphi Options, Library-Win32, Library path". Then you do not need to specify anything in Project Options when you need to use the unit, just add GifImage to your program's uses clause. By doing this you can only load and display GIF images at runtime, not at designtime. If you want to add the unit to a design time package in order to enable you to load gif files at design time in the Delphi IDE, this is the steps you need to follow: 1. Create a directory where you place the GIFImage.pas unit. Example directory: C:\D2006ComponentsWin32\GifImage Place the GIFImage.pas unit in this directory. 2. Start BDS 2006. 3. In Tools, Options..., Delphi Options, Library-Win32, add the directory C:\D2006ComponentsWin32\GifImage to Library path, click OK. 4. Start BDS 2006 and select the menu items File, New, Other, Delphi Projects, Select the Package icon and click OK. 5. In Project Manager, right click the Contains node and select Add... 6. In "Unit file name" type the full path for the GIFImage.pas or use the Browse button. Click Ok. 7. The Contains node should contain the GIFImage.pas unit. 8. In Project Manager, right click the package name (Package##.bpl) and select Options... 9. In Description type "GIFImage Designtime Package". Select the "Designtime only" radiobutton. Click OK. 10. In Project Manager, right click the package name (Package##.bpl) and select Save As... 11. Save the package as "GIFImageD2006.bdsproj" in the C:\D2006ComponentsWin32\GifImage directory. 12. In Project Manager, right click the GIFImageD2006.bpl and select Build. 13. On a successful Build, right click the GIFImageD2006.bpl and select Install. 14. You should now get a dialog box telling you that the package has been installed. Click OK. 15. Select File, Close All. Note that since TGIFImage is not a component, it will not show in the component palette. Test that it has been installed properly. New Project, Delphi Projects, VCL Forms Application. Put a TImage on the form. In the Object Inspector, Picture property, click the ellipsis button. In Picture Editor, click Load... and select a GIF file and click Open, and click OK. The GIF should now be displayed on the form. ----- How do you use the GifImage unit? Simply add the GifImage unit to one the uses clauses in your program. Then you can load GIF files into a TImage component the same way you load BMP files. uses GifImage; procedure TForm1.Button1Click(Sender: TObject); begin Image.Picture.LoadFromFile('C:\SomeDir\SomeFile.gif'); end; ----- How to display animated GIF picture on the form? Put GifImage in your uses clause in the program. Then you can use Delphi's TImage component to load animated gif pictures. ----- I know how to load a GIF file from the harddrive using Image1.Picture.LoadFromFile but is it possible to load a GIF file from a resource instead? Create a resource file (*.rc) containing a line like this: RESNAME RCDATA "filename.gif" You can then load the GIF from the resource in 2 ways: 1. Use a TResourceStream var ResStream: TResourceStream; Gif: TGifImage; begin ResStream := TResourceStream.Create(HInstance, 'RESNAME', RT_RCDATA); Gif := TGifImage.Create; try Gif.LoadFromStream(ResStream); Image.Picture.Assign(nil); Image.Picture.Assign(Gif); finally ResStream.Free; Gif.Free; end; end; 2. Use TGifImage.LoadFromResourceName var Gif: TGifImage; begin Gif := TGifImage.Create; try Gif.LoadFromResourceName(HInstance, 'RESNAME'); Image1.Picture.Assign(nil); Image1.Picture.Assign(Gif); finally Gif.Free; end; end; ----- I used Tgifimage for Delphi 5, but i can't get it working in Delphi 6/7. Can jou tell me how to use Tgifimage in Delphi 6/7? You use it the same way in Delphi 6/7 as in Delphi 5. No difference. Just be sure that you use the latest Delphi 7 compatible version. You can use the latest Delphi 7 version in previous Delphi versions. It should be backwards compatible, although it is not tested much. If you are in doubt, just use the GifImage version that corresponds with your Delphi version. ----- Is it possible to insert 5 or 6 different GIF images in to delphi image component? Use one TImage for each GIF image you want to show. ----- How do I load a GIF image to a TBitMap? To convert a GIF file to a bitmap you first Create a Bitmap object and then assign the gifimage to it. var Bitmap: TBitmap; GifImage: TGifImage; begin GifImage := TGifImage.Create; Bitmap := TBitmap.Create; try GifImage.LoadFromFile('C:\SomeDir\SomeFile.gif'); Bitmap.Assign(GifImage); // do something with Bitmap here finally GifImage.Free; Bitmap.Free; end; end; ----- I can't figure out (after spending a long time reading the help and looking at the demo code) how to get the index of the current frame in an animated gif. This would be very useful in order make a text comment in a memo when a certain frame is played thereby synchronizing text and gif. Is there a way to get the frame number currectly displayed? To display the frame number of an animated GIF file: procedure TForm1.Button1Click(Sender: TObject); begin if OpenPictureDialog.Execute then begin Image.Picture.Assign(nil); FrameLabel.Caption := ''; Image.Picture.LoadFromFile(OpenPictureDialog.FileName); if (Image.Picture.Graphic is TGifImage) then TGifImage(Image.Picture.Graphic).OnPaint := OnGifPaint; end; end; procedure TForm1.OnGifPaint(Sender: TObject); var ai: Integer; begin ai := (Sender as TGIFPainter).ActiveImage; FrameLabel.Caption := IntToStr(ai); FrameLabel.Refresh; end; ----- Is it possible to make an animated Gif start at a certain frame? To start an animated GIF at a specific frame you can do this: Image.Picture.LoadFromFile(OpenPictureDialog.FileName); if (Image.Picture.Graphic is TGifImage) then begin TGifImage(Image.Picture.Graphic).OnStartPaint := OnGifStartPaint; TGifImage(Image.Picture.Graphic).OnEndPaint := OnGifEndPaint; end; procedure TForm1.OnGifStartPaint(Sender: TObject); begin // Setting ActiveImage to a value <> 0 makes the gif play only once // I haven't really looked into why (Sender as TGIFPainter).ActiveImage := 30; // start with frame number 30 end; procedure TForm1.OnGifEndPaint(Sender: TObject); begin // Restart the gif so it plays all the time. (Sender as TGIFPainter).Image.Animate := False; (Sender as TGIFPainter).Image.Animate := True; end; ----- Is there any quick way of setting a property (or maybe introducing a new property), so I can tell TGifImage which frame to display? In this case i want to display just 1 frame of the many frames of a GifImage. If the GifImage is not animating, it only displays the first frame. I want to tell which frame to display. You can display a specific frame from an animated GIF file in several ways. 1. Extract the single frame you want as a bitmap and display that bitmap in a TImage. This is the simplest if you do not want the gif to animate anyway. procedure TForm1.Button1Click(Sender: TObject); var Gif: TGIFImage; FrameNumber: Integer; begin Image.Picture.Assign(nil); Gif := TGIFImage.Create; try Gif.LoadFromFile(SomeGifFileName); FrameNumber := 30; // the framenumber you want Image.Picture.Assign(Gif.Images.SubImages[FrameNumber].Bitmap); finally Gif.Free; end; end; 2. Use the OnPaint event to let a TImage display the same frame over and over again. procedure TForm1.Button1Click(Sender: TObject); begin Image.Picture.Assign(nil); Image.Picture.LoadFromFile(SomeGifFileName); if (Image.Picture.Graphic is TGifImage) then TGifImage(Image.Picture.Graphic).OnPaint := OnGifPaint; end; procedure TForm1.OnGifPaint(Sender: TObject); begin (Sender as TGIFPainter).ActiveImage := 30; end; 3. Use the ForceFrame property which is a recent addition to the GifImage unit. How to use: Set the Animate property to False and set the ForceFrame property to a desired frame number (0-N). Normal display: Set the ForceFrame property to -1 and set Animate to True. Example, show a specific frame: TGifImage(Image1.Picture.Graphic).Animate := False; TGifImage(Image1.Picture.Graphic).ForceFrame := 0; Example, show GIF animated again: TGifImage(Image1.Picture.Graphic).Animate := True; TGifImage(Image1.Picture.Graphic).ForceFrame := -1; ----- How do I create a GIF with a transparent background? Here is some sample code: uses GifImage; procedure TForm1.Button1Click(Sender: TObject); var GIFimage : TGIFimage; SubImage : TGIFsubImage; GCE : TGIFgraphicControlExtension; Bitmap : TBitmap; TransparentColor: TColor; idx, x, y, TransparentIndex: Integer; begin // create bitmap with diagonal red line on yellow background Bitmap := Tbitmap.Create; try Bitmap.PixelFormat := pf8bit; // there can be max 256 colours in the gif image anyway Bitmap.Width := 30; Bitmap.Height := 30; Bitmap.Canvas.Brush.Color := clYellow; Bitmap.Canvas.Brush.Style := bsSolid; Bitmap.Canvas.FillRect(rect(0,0,30,30)); Bitmap.Canvas.Pen.Color := clRed; Bitmap.Canvas.MoveTo(0,0); Bitmap.Canvas.LineTo(30,30); // create GIF image and assign bitmap to subimage; set transparency and -color to yellow GIFimage:=TGIFimage.Create; try GIFimage.Add(Bitmap); // -> find the subimage Subimage := GIFimage.Images[0]; // 0=the first image // remove unused entries from the colour palette. Not necessary. subimage.ColorMap.Optimize; { GCE := TGIFgraphicControlExtension.Create(SubImage); GCE.Transparent := True; GCE.TransparentColor := clYellow; // -> add the control extension to the gif Subimage.Extensions.Add(GCE); Specifying TransparentColor does not always work reliably. You should use TransparentColorIndex instead. The problem is that using GCE.TransparentColor to specify the transparent color is not always reliable. It may fail in situations where where the colour palette has more than one entry with the transparent colour. And sometimes TGIFImage creates a colour palette with some colours being present more than once. TGIFImage has to lookup the colour you specify in the subimage's colour palette to find the palette entry index and it stops as soon as it find a matching colour. Unfortunately this is not always good enough, because it also has to be an index entry which is actually used in the image, otherwise that transparent color index is simply ignored. The colour map can contain colour entries which are not used by the image. The solution is to specify GCE.TransparentColorIndex instead. } TransparentColor := clYellow; // find the transparent colour in the palette TransparentIndex := -1; for idx := 0 to Subimage.ColorMap.Count - 1 do begin if Subimage.ColorMap.Colors[idx] = TransparentColor then // now check that this palette index is actually iused in the image for y := 0 to Subimage.Height - 1 do begin for x := 0 to Subimage.Width - 1 do if Subimage.Pixels[x, y] = idx then begin TransparentIndex := idx; Break; end; if TransparentIndex >= 0 then Break; end; if TransparentIndex >= 0 then Break; end; // did we find a useable palette index for the transparent colour? if TransparentIndex >= 0 then begin // -> create a control extension to specify transparent colour GCE := TGIFgraphicControlExtension.Create(Subimage); GCE.TransparentColorIndex := TransparentIndex; GCE.Transparent := True; // -> add the control extension to the gif Subimage.Extensions.Add(GCE); end; // GIFimage.SaveToFile('C:\30x30.gif'); Image1.Picture.Assign(GIFimage); finally GIFimage.Free; end; finally Bitmap.Free; end; end; ----- I am working with TGifImage on a win xp system with lots of Ram. (>500) If I use a win98 system with only 128 mb of ram, it crashes in the designer. What is the problem? The problem is not the amount of RAM. The problem is that you are running out of GDI resources, when you are using Delphi 7 on a Windows playstation (Windows 9x). Windows 95/98/ME is not suitable for serious work. You should only use Windows 2000 or XP to run Delphi when developing programs. There are three factors that influence you situation: 1. The Delphi IDE use a lot a resources and Delphi 7 even more than previous versions. 2. The TGIFImage also uses resources, especially with animated gif files. 3. Windows 9x has only a very limited amount of resources available. Now combine these factors and it will quickly becomes a serious problem if you are trying to use Delphi 7 to develop an application that uses a lot of resources on win9x. Btw. the resources I'm talking about is not the amount of available memory, but primarily the number of GDI resources which is very limited in win9x. You can see how many resources are used in Windows 98 by running the resource meter program (whatever it's called in english) which is a standard windows app in the start menu under accessories. If you close all programs including Delphi and run your program alone, it might just run fine in Windows 98. Develop only on a Windows NT platform and test the app on Win9x by running it normally. ----- Does TGifImage work in Kylix on the Linux platform? Does TGifImage work in .NET? What Delphi versions does TGifImage work with? It does not work in Kylix / Linux. It does not work in .NET. TGifImage only works in Delphi on the win32 Windows platform. I have no intention of making it work in other platforms. TGifImage works with Delphi 2, 3, 4, 5, 6, 7, 2005, 2006, 2007, 2009 and C++ Builder 3 & 4. -----