Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7859677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:58:55+00:00 2026-06-02T21:58:55+00:00

I am using TGifImage that is included with Delphi XE. What I am trying

  • 0

I am using TGifImage that is included with Delphi XE.

What I am trying to do is load a Gif from a File and and extract all the frames to a Bitmap.

This is what I did so far:

procedure ExtractGifFrames(FileName: string);
var
  Gif: TGifImage;
  Bmp: TBitmap;
  i: Integer;
begin
  Gif := TGifImage.Create;
  try
    Gif.LoadFromFile(FileName);

    Bmp := TBitmap.Create;
    try
      Bmp.SetSize(Gif.Width, Gif.Height);

      for i := 0 to Gif.Images.Count - 1 do
      begin
        if not Gif.Images[i].Empty then
        begin
          Bmp.Assign(Gif.Images[i]);
          Bmp.SaveToFile('C:\test\bitmap' + IntToStr(i) + '.bmp');
        end;
      end;
    finally
      Bmp.Free;
    end;
  finally
    Gif.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
  begin
    ExtractGifFrames(OpenPictureDialog1.FileName);
  end;
end;

The problem I am facing is with some transparency issue with a lot of different Gifs, and also size problems.

Here are some example bitmaps that were saved using my code above:

enter image description here
enter image description here

As you can see the results are not great, they have size and transparency issues.

I know the Gif Files themselves are not corrupt, because I can load them through my web browser and they display correctly without fault.

How can I load a Gif from File, assign each frame to Bitmap without losing any quality?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:59:00+00:00Added an answer on June 2, 2026 at 9:59 pm

    For older Delphi Versions (Pre 2009): Take a look at the code of GIFImage unit, you might want to check how TGIFPainter renders the images based on each Frame’s Disposal method.

    I have wrote a small code utilizing TGIFPainter.OnAfterPaint event handler to save the active frame to BMP, and do all the “hard work”.

    Note: GIFImage unit version 2.2 Release: 5 (23-MAY-1999)

    type
      TForm1 = class(TForm)
        Button1: TButton;
        ProgressBar1: TProgressBar;
        procedure Button1Click(Sender: TObject);
      public
        FBitmap: TBitmap;
        procedure AfterPaintGIF(Sender: TObject);
      end;
    
    ...
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      GIF: TGIFImage;
    begin
      GIF := TGIFImage.Create;
      FBitmap := TBitmap.Create;
      Button1.Enabled := False;
      try
        GIF.LoadFromFile('c:\test\test.gif');
        GIF.DrawOptions := GIF.DrawOptions - [goLoop, goLoopContinously, goAsync];
        GIF.AnimationSpeed := 1000; // Max - no delay
        FBitmap.Width := GIF.Width;
        FBitmap.Height := GIF.Height;
        GIF.OnAfterPaint := AfterPaintGIF;
    
        ProgressBar1.Max := Gif.Images.Count;
        ProgressBar1.Position := 0;
        ProgressBar1.Smooth := True;
        ProgressBar1.Step := 1;
    
        // Paint the GIF onto FBitmap, Let TGIFPainter do the painting logic
        // AfterPaintGIF will fire for each Frame
        GIF.Paint(FBitmap.Canvas, FBitmap.Canvas.ClipRect, GIF.DrawOptions);
        ShowMessage('Done!');
      finally
        FBitmap.Free;
        GIF.Free;
        Button1.Enabled := True;
      end;
    end;
    
    procedure TForm1.AfterPaintGIF(Sender: TObject);
    begin
      if not (Sender is TGIFPainter) then Exit;
      if not Assigned(FBitmap) then Exit;
      // The event will ignore Empty frames      
      FBitmap.Canvas.Lock;
      try
        FBitmap.SaveToFile(Format('%.2d.bmp', [TGIFPainter(Sender).ActiveImage]));
      finally
        FBitmap.Canvas.Unlock;
      end;
      ProgressBar1.StepIt;
    end;
    

    Note: No error handling to simplify the code.

    output bitmaps


    For newer Delphi Versions (2009+): With build-in GIFImg unit, you can do this quit easy with the use of TGIFRenderer (which completely replaced old TGIFPainter) e.g.:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      GIF: TGIFImage;
      Bitmap: TBitmap;
      I: Integer;
      GR: TGIFRenderer;
    begin
      GIF := TGIFImage.Create;      
      Bitmap := TBitmap.Create;
      try
        GIF.LoadFromFile('c:\test\test.gif');
        Bitmap.SetSize(GIF.Width, GIF.Height);
        GR := TGIFRenderer.Create(GIF);
        try
          for I := 0 to GIF.Images.Count - 1 do
          begin
            if GIF.Images[I].Empty then Break;
            GR.Draw(Bitmap.Canvas, Bitmap.Canvas.ClipRect);
            GR.NextFrame;
            Bitmap.SaveToFile(Format('%.2d.bmp', [I]));
          end;
        finally
          GR.Free;
        end;  
      finally
        GIF.Free;
        Bitmap.Free;
      end;
    end;
    

    Using GDI+:

    uses ..., GDIPAPI, GDIPOBJ, GDIPUTIL;
    
    procedure ExtractGifFrames(const FileName: string);
    var
      GPImage: TGPImage;
      encoderClsid: TGUID;
      BmpFrame: TBitmap;
      MemStream: TMemoryStream;
      FrameCount, FrameIndex: Integer;
    begin
      GPImage := TGPImage.Create(FileName);
      try
        if GPImage.GetLastStatus = Ok then
        begin
          GetEncoderClsid('image/bmp', encoderClsid);
          FrameCount := GPImage.GetFrameCount(GDIPAPI.FrameDimensionTime);
          for FrameIndex := 0 to FrameCount - 1 do
          begin
            GPImage.SelectActiveFrame(GDIPAPI.FrameDimensionTime, FrameIndex);
            MemStream := TMemoryStream.Create;
            try
              if GPImage.Save(TStreamAdapter.Create(MemStream), encoderClsid) = Ok then
              begin
                MemStream.Position := 0;
                BmpFrame := TBitmap.Create;
                try
                  BmpFrame.LoadFromStream(MemStream);
                  BmpFrame.SaveToFile(Format('%.2d.bmp', [FrameIndex]));
                finally
                  BmpFrame.Free;
                end;
              end;
            finally
              MemStream.Free;
            end;
          end;
        end;
      finally
        GPImage.Free;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Java,I have to fetch multiple sets of values from an XML file to
Question: Why does my .gif load, but not play when updating using GifSourcePropertyChanged ?
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using Android 2.1+. I have a service that gets killed from time to time
I have yet another problem regarding GifImage.pas that is included with Delphi XE. The
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using CI for the first time and i'm smashing my head with this seemingly
Using the Redis info command, I am able to get all the stats of
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
Using linq2sql I'm trying to take the string in txtOilChange and update the oilChange

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.