gpt4 book ai didi

delphi - 如何将 TGifImage 中的帧提取为位图?

转载 作者:行者123 更新时间:2023-12-03 14:52:42 26 4
gpt4 key购买 nike

下面的演示尝试在表单的 Canvas 上绘制 GIF。这不起作用。图像不会前进。如何让它发挥作用?

procedure TForm1.FormCreate(Sender: TObject);
begin
GIF := TGIFImage.Create;
GIF.LoadFromFile('c:\2.gif');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
GIF.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
VAR i: Integer;
begin
REPEAT
for i:= 0 to GIF.Images.Count-1 DO
begin
DisplayGifFrame(i);
Sleep(30);
end;
UNTIL FALSE;
end;


procedure TForm1.DisplayGifFrame(AIndex: Integer);
VAR
Renderer: TGIFRenderer;
begin
Renderer := TGIFRenderer.Create(GIF);
TRY
Renderer.Animate := TRUE;
Renderer.FrameIndex := AIndex;
Renderer.Draw(Canvas, Canvas.ClipRect);

{save frame in a bitmap here. save bitmap to disk}
// xxx
FINALLY
Renderer.Free;
end;
end;

我想要实现的目标:
我想从 GIF 中提取所有帧并将每个帧放入唯一的位图中。

<小时/>

更新:
继续:Cannot draw GIF on dynamically created TBitmap(s)

最佳答案

试试这个:

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;

上面的代码考虑了Frame的Disposal方法。查看相关问题here .

关于delphi - 如何将 TGifImage 中的帧提取为位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15136993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com