如何在Delphi 2009中创建GIF动画文件?

5
gif := TgifImage.Create;
gif.Width := 100;
gif.Height := 100;
gif.AnimationSpeed := 500;
gif.Animate := true;
gif.add(image1.Picture.Bitmap);
gif.add(image2.Picture.Bitmap);
gif.add(image3.Picture.Bitmap);
gif.SaveToFile('gif.gif');

这个循环只执行一次,速度不是500吗?

如何使其循环并设置速度?

4个回答

6
Anders Melander,原创TGIFImage的作者,给出了以下答案

您需要在GIF的第一帧中添加“Netscape Loop”扩展块。 循环块必须是您为该帧定义的第一个扩展块,否则它将无法工作。

请参见动画演示,了解如何构建动画GIF的示例。

这是动画演示的代码摘录:

// Add the source image to the animation
Result := GIF.Add(Source);

// Netscape Loop extension must be the first extension in the first frame!
if (GIF.Images.Count = 1) then
begin
  LoopExt := TGIFAppExtNSLoop.Create(Result);
  LoopExt.Loops := 0; // Number of loops (0 = forever)
end;

You can view the TGIFImage documentation here.


为了清晰起见,您应该指定LoopExt的类型。 - Andreas Rejbrand

2
var Gif:TGifImage;
begin
    //Setting the delay for each frame
    TGIFGraphicControlExtension.Create(Gif.Add(image1.Picture.Bitmap)).Delay := 300;
    TGIFGraphicControlExtension.Create(Gif.Add(image2.Picture.Bitmap)).Delay := 300;
    TGIFGraphicControlExtension.Create(Gif.Add(image3.Picture.Bitmap)).Delay := 300;
    //Adding loop extension in the first frame (0 = forever)
    TGIFAppExtNSLoop.Create(Gif.Images.Frames[0]).Loops := 0;

    Gif.SaveToFile('gif.gif');
end;

1
直到我意识到它已经损坏了。显然,在第一个GCE之前必须插入NS循环。请参见https://dev59.com/u18e5IYBdhLWcg3wssAV#25480538。 - Andreas Rejbrand

1
您可以在我的主页www.tolderlund.eu/delphi/上看到如何创建动画GIF的示例。此外,还有适用于Delphi 5、Delphi 6、Delphi 7、Delphi 2005、Delphi 2006、Delphi 2007、Delphi 2009的原始TGIFImage。

-1

至少需要一个计时器和一些无闪烁方法。

可查看 RXLibrary 中的单元 rxAnimate.pas 的示例(免费提供。源代码在 SourceForge.net 或 http://www.dummzeuch.de/delphi/english.html 上可得)。

JVCL 也有类似组件的源代码。


哎呀...抱歉,我没有看到它是TGifImage。我原本想创建一个gif动画组件。 - volvox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接