3

I need to obtain the first frame from an animated GIF file. This is easy to achieve by loading the file in a TGIFImage object:

  GIF.LoadFromFile(FileName)
  BMP.Assign(GIF)

The problem is that for (large) animated GIF files this takes a while... my computer needs 12 seconds and a LOT of RAM to load a 50MB file.

I was hoping that I will be able to pass the file to TCustomGIFRenderer and extract the first frame. But TCustomGIFRenderer only operates on 'live' (in memory) GIF images.

Is it possible to get only the first frame without loading the whole file?

Gabriel
  • 20,797
  • 27
  • 159
  • 293
  • I wouldn't be surprised if you could implement your own `TCustomGIFRenderer` variation which reads from a file stream. – Jerry Dodge Jun 02 '17 at 15:33
  • 2
    maybe I should write a 'hack' class for TGIFImageList in which I override the LoadFromStream(Stream: TStream) and stop reading from the GIF file immediately after I read the first frame? – Gabriel Jun 02 '17 at 15:42
  • I'd look for a third party library that could do this. – David Heffernan Jun 03 '17 at 08:01

1 Answers1

2

Have you tried GDI+.

uses GDIPAPI, GDIPOBJ, GDIPUTIL;

procedure TForm1.Button1Click(Sender: TObject);
var
  GPImage: TGPImage;
  GPGraphics: TGPGraphics;
  Bitmap: TBitmap; // first frame
begin
  GPImage := TGPImage.Create('D:\animated-tiger.gif');
  try
    Bitmap := TBitmap.Create;
    try
      Bitmap.Width := GPImage.GetWidth;
      Bitmap.Height := GPImage.GetHeight;
      GPGraphics := TGPGraphics.Create(Bitmap.Canvas.Handle);
      try
        GPGraphics.DrawImage(GPImage, 0, 0, Bitmap.Width, Bitmap.Height);
        Image1.Picture.Assign(Bitmap);
      finally
        GPGraphics.Free;;
      end;
    finally
      Bitmap.Free;
    end;
  finally
    GPImage.Free;
  end;
end;

You might also want to try TWICImage (for newer Delphi version). For older Delphi version which do not have TWICImage build-in, see this Q: Delphi 2007 using Windows Imaging Component (WIC).

In both cases (GDI+/WIC) only the first GIF frame will be extracted.

kobik
  • 21,001
  • 4
  • 61
  • 121