Delphi 带有动画GIF的TGIFImage-事件不起作用-如何检测动画进度?

Delphi 带有动画GIF的TGIFImage-事件不起作用-如何检测动画进度?,delphi,tgifimage,Delphi,Tgifimage,Delphi的tgiImage具有以下事件:OnPaint,OnAfterPaint,OnLoop,OnEndPaint。 但在显示动画GIF时,这些事件都不会被调用 我使用以下代码显示动画GIF: FGif := (Image1.Picture.Graphic as TGIFImage); FGif.OnProgress := GifProgress; FGif.OnLoop := GifLoop; FGif.OnPaint := GifPaint; FGif.OnEndP

Delphi的
tgiImage
具有以下事件:
OnPaint
OnAfterPaint
OnLoop
OnEndPaint
。 但在显示动画GIF时,这些事件都不会被调用

我使用以下代码显示动画GIF:

  FGif := (Image1.Picture.Graphic as TGIFImage);
  FGif.OnProgress := GifProgress;
  FGif.OnLoop := GifLoop;
  FGif.OnPaint := GifPaint;
  FGif.OnEndPaint := GifEndPaint;
  FGif.OnAfterPaint := GifAfterPaint;
  FGif.Animate := True;
播放动画时如何提取当前可见帧索引

如何检测动画何时完成

如何检测下一帧何时显示

OnProgress
事件仅在绘制最后一个动画帧后的第一个动画循环期间调用-TGIFImage将从第一个帧继续动画,但此事件停止工作


我使用的是Delphi 10.2 Tokyo。

您提到的大多数事件都没有(正如您所发现的)实现,尽管占位符在那里。大约十年前,当对
GifImg
单元进行重大修改时,它们似乎已经消失了

但是,通过另一种方法,您可以解决您提到的问题。也就是说,分别声明
TGIFRenderer
,这样您就可以访问所需的信息

示例应用程序如下所示:

type
  TForm22 = class(TForm)
    Button1: TButton;
    OpenDlg: TOpenDialog;
    SaveDlg: TSaveDialog;
    Image1: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    r: TRect;
    gif: TGifImage;
    rend: TGIFRenderer;
    procedure GifChange(Sender: TObject);
  public
  end;

implementation


procedure TForm22.Button1Click(Sender: TObject);
begin
  if not OpenDlg.Execute() then Exit;

  // the following is 12 by default to be as slow as Mozilla in last millenium
  GIFDelayExp := 10; // set to 10 for correct timing

  gif:= TGIFImage.Create;
  gif.LoadFromFile(OpenDlg.FileName);
  gif.OnChange := GifChange;

  r := Rect(0, 0, Gif.Width, Gif.Height);
  r.offset((Image1.Width-Gif.Width) div 2, (Image1.Height-Gif.Height) div 2);

  rend := TGIFRenderer.Create(Gif);
  rend.Animate := True;
  rend.StartAnimation;
  rend.Draw(Image1.Canvas, r);

  //Set animate to true at the end, otherwise an exception at address will be raised.
  gif.Animate := True;
end;

procedure TForm22.GifChange(Sender: TObject);
begin
  rend.Draw(Image1.Canvas, r);

  Label1.Caption := Format('Frame nr %d / %d',[rend.FrameIndex, gif.Images.Count]);
  Label2.Caption := Format('Per frame: %d ms',[rend.FrameDelay]);
  Label3.Caption := Format('Full cycle: %d s',[rend.FrameDelay * gif.Images.Count]);
end;

使用
TGifRenderer.FrameIndex
TGifImage.Images.Count
TGifRenderer.FrameDelay
可以计算问题的答案。

如果您有权访问源代码,可以进行一些调试,以找出不调用事件的原因