Extract the first frame from a large GIF (at the optimal time)

I need to get the first frame from an animated GIF file. This can be easily achieved by loading the file into a TGIFImage object:

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

      

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

I was hoping I could pass the TCustomGIFRenderer file and extract the first frame. But TCustomGIFRenderer only works with live (in memory) GIF images.

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

+3


source to share


1 answer


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 can also try TWICImage (for newer Delphi version). For an older version of Delphi that doesn't have a built-in module TWICImage

, see this question: Delphi 2007 using the Windows Imaging Component (WIC) .

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

+2


source







All Articles