How to extract frames from a TGifImage into Bitmaps?

前端 未结 1 492
小蘑菇
小蘑菇 2021-02-09 10:10

The demo below tries to draw the GIF on form\'s canvas. It doesn\'t work. The image won\'t advance. How to make it work?

procedure TForm1.FormCreate(Sender: TObj         


        
1条回答
  •  醉酒成梦
    2021-02-09 10:21

    Try this:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      GIF: TGIFImage;
      Bitmap: TBitmap;
      I: Integer;
      GR: TGIFRenderer;
      R: TRect;
    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;
    

    The above code takes into account Frame's Disposal method. see related question here.

    0 讨论(0)
提交回复
热议问题