我有这个功能,从动画GIF中提取帧.它适用于除此之外的所有GIF:
{ Loads a GIF. Returns a list of BMP frames }
function GetGifFrames(GifFile: string): TObjectList;
var
GIF: TGIFImage;
TempFrame: TBitmap;
Frame: TBitmap;
Counter: Integer;
GR: TGIFRenderer;
begin
{ Load GIF }
GIF := TGIFImage.Create;
TRY
TRY
Gif.Animate := FALSE;
GIF.LoadFromFile(GifFile);
EXCEPT
MesajError('Cannot load '+ GifFile);
EXIT(NIL);
END;
if Gif.Images.Count= 1 then
begin
MsgError('This is not an animated GIF'+ CRLF+ GifFile);
EXIT(NIL);
end;
Result:= TObjectList.Create;
Result.OwnsObjects:= TRUE; { Array of images }
{ GIF render }
TempFrame:= TBitmap.Create;
GR:= TGIFRenderer.Create(GIF); { GIF render }
TRY
TempFrame.SetSize(GIF.Width, GIF.Height);
for Counter:= 0 to GIF.Images.Count-1 DO
begin
{ Skip bad frames }
if GIF.Images[Counter].Empty
then Continue;
{ Create new frame }
Frame:= TBitmap.Create;
Frame.SetSize(GIF.Width, GIF.Height);
GR.Draw(TempFrame.Canvas, TempFrame.Canvas.ClipRect); <---------- AV here { Transfer image from GIF to BMP }
Frame.Assign(TempFrame);
Result.Add(Frame); { Add to list of bitmap frames }
GR.NextFrame; { Advance }
end;
FINALLY
FreeAndNil(GR);
FreeAndNil(TempFrame);
END;
FINALLY
FreeAndNil(GIF);
END;
end;
我在上面的线上有一个AV
Debugger Exception Notification Project Tester.exe raised exception
class $C0000005 with message ‘access violation at 0x005d3924: read of
address 0x0000002c’.
堆栈跟踪是:
GetGifFrames('C:\Test gif\err.gif')
GIFImg.TGIFRenderer.Draw($7EFA9070,(0, 0, 108, 146, (0, 0), (108, 146)))
GIFImg.TGIFRenderer.GetBitmap
GIFImg.TGIFRenderer.RenderFrame
在Render框架中,它在此行崩溃:
PreviousBuffer.Canvas.CopyRect(PreviousBuffer.Canvas.ClipRect, Buffer.Canvas, Buffer.Canvas.ClipRect);
这是因为PreviousBuffer是NIL !!!!
如何解决这个问题?
最佳答案 GIF文件中帧的一个属性是DisposalMethod,它定义了帧的图像应该如何处理以准备下一帧.在原始文件(帖子顶部的“Sansanimated.gif”链接)中,对于所有13帧,这被设置为dmNoDisposal.这在您的代码中可以正常工作而没有问题.在文件“err.gif”中,两个帧都具有dmPrevious,这需要额外的内部位图.如果GIFRenderer未初始化为动画,则不会分配此位图.
要使用dmPrevious处理方法设置为框架正确初始化GIFRenderer,请添加一行
GR.Animate := True; // <---- add this line
创建GR之后.