I have yet another problem regarding GifImage.pas that is included with Delphi XE. The issue I have sort of relates to a previous question I asked: TGifImage Transparency Issue
The problem from the previous question above was solved by using the TGifRenderer to display the correct frames from a Gif file which worked perfectly for loading.
But now I need to do the opposite, I need to rebuild and save a new Gif from a series of bitmaps. But when I save, the output Gif file draws incorrectly – just like before.
I am not sure if I need TGIfRenderer again or not because all I am doing is adding bitmaps and packing then saving the Gif.
To see the exact problem I am facing, please carry on reading (apologies in advance for the long post!)
First save these bitmaps to a new sample project folder:
NOTE: These images were originally bitmaps but StackOverflow seems to have converted them to PNG so you might need to copy into Paint and save again.




Now from a new project, paste the following code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, GifImg, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Bmp: TBitmap;
Gif: TGIFImage;
begin
Bmp := TBitmap.Create;
Gif := TGIFImage.Create;
try
Bmp.LoadFromFile('img0.bmp');
Bmp.Transparent := True;
Gif.Add(Bmp);
Bmp.LoadFromFile('img1.bmp');
Bmp.Transparent := True;
Gif.Add(Bmp);
Bmp.LoadFromFile('img2.bmp');
Bmp.Transparent := True;
Gif.Add(Bmp);
Bmp.LoadFromFile('img3.bmp');
Bmp.Transparent := True;
Gif.Add(Bmp);
// add netscape loop if we want animation to keep repeating
TGIFAppExtNSLoop.Create(Gif.Images.Frames[0]).Loops := 0;
Gif.Pack;
Gif.SaveToFile('test.gif');
finally
Bmp.Free;
Gif.Free;
end;
end;
end.
Make sure the saved bitmaps are in the same folder as the project’s exe (or change the filename paths) then run the project to create the output gif.
The saved output is not correct. From the penguin bitmaps saved above, I get this saved Gif:

You can see the frames are overlapping each other or something funky is going on! The source to this gif file can be found from here: http://www.animated-gifs.org.uk/agif/ani02.gif
I did not use all the frames from the Gif as there is so many, but you can see how it should work when animated.
Note that this does not happen with every gif, only a select few seem to save like this. Another example would be to try these bitmaps instead of the penguin:




The result is:

which should be (notice the flames on the plane):

To put it simply, I need to save a Gif from a series of bitmaps to be just like how they were when they were opened. You can see from the sample bitmaps above there is no inconsistency with the actual image data, so this makes me believe it is something to do with how they are added to the gif.
If I do need TGIfRenderer again I am not sure how to implement it for saving, I keep thinking it is for loading only?
This is one of those issues I get that I cannot solve. I’ve been trying to change the transparency and color and mode etc but I cannot seem to figure it out.
I’m not an expert with GIFImg or gif images but the problem seems to be related with disposal of frames. Searching for ‘disposal’ in ‘GIFImg.pas’, I came up with the below which seems to work for your test image: