delphi - TGifImage 透明度问题

标签 delphi delphi-xe

我正在使用 Delphi XE 中包含的 TGifImage。

我想做的是从文件加载 Gif,并将所有帧提取到位图。

这是我到目前为止所做的:

procedure ExtractGifFrames(FileName: string);
var
  Gif: TGifImage;
  Bmp: TBitmap;
  i: Integer;
begin
  Gif := TGifImage.Create;
  try
    Gif.LoadFromFile(FileName);

    Bmp := TBitmap.Create;
    try
      Bmp.SetSize(Gif.Width, Gif.Height);

      for i := 0 to Gif.Images.Count - 1 do
      begin
        if not Gif.Images[i].Empty then
        begin
          Bmp.Assign(Gif.Images[i]);
          Bmp.SaveToFile('C:\test\bitmap' + IntToStr(i) + '.bmp');
        end;
      end;
    finally
      Bmp.Free;
    end;
  finally
    Gif.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
  begin
    ExtractGifFrames(OpenPictureDialog1.FileName);
  end;
end;

我面临的问题是许多不同 Gif 的透明度问题,以及尺寸问题。

以下是使用我上面的代码保存的一些示例位图:

enter image description here enter image description here

正如您所看到的,结果并不理想,它们存在大小和透明度问题。

我知道 Gif 文件本身没有损坏,因为我可以通过网络浏览器加载它们,并且它们可以正确显示而不会出现错误。

如何从文件加载 Gif,将每个帧分配给位图而不损失任何质量?

最佳答案

对于较旧的 Delphi 版本(2009 年之前): 看一下 GIFImage 单元的代码,您可能想检查 TGIFPainter根据每个框架的 Disposal 方法渲染图像。

我使用 TGIFPainter.OnAfterPaint 事件处理程序编写了一段小代码,将事件帧保存到 BMP,并完成所有“艰苦的工作”。

注意:GIFImage 单元版本 2.2 发布:5(1999 年 5 月 23 日)

type
  TForm1 = class(TForm)
    Button1: TButton;
    ProgressBar1: TProgressBar;
    procedure Button1Click(Sender: TObject);
  public
    FBitmap: TBitmap;
    procedure AfterPaintGIF(Sender: TObject);
  end;

...

procedure TForm1.Button1Click(Sender: TObject);
var
  GIF: TGIFImage;
begin
  GIF := TGIFImage.Create;
  FBitmap := TBitmap.Create;
  Button1.Enabled := False;
  try
    GIF.LoadFromFile('c:\test\test.gif');
    GIF.DrawOptions := GIF.DrawOptions - [goLoop, goLoopContinously, goAsync];
    GIF.AnimationSpeed := 1000; // Max - no delay
    FBitmap.Width := GIF.Width;
    FBitmap.Height := GIF.Height;
    GIF.OnAfterPaint := AfterPaintGIF;

    ProgressBar1.Max := Gif.Images.Count;
    ProgressBar1.Position := 0;
    ProgressBar1.Smooth := True;
    ProgressBar1.Step := 1;

    // Paint the GIF onto FBitmap, Let TGIFPainter do the painting logic
    // AfterPaintGIF will fire for each Frame
    GIF.Paint(FBitmap.Canvas, FBitmap.Canvas.ClipRect, GIF.DrawOptions);
    ShowMessage('Done!');
  finally
    FBitmap.Free;
    GIF.Free;
    Button1.Enabled := True;
  end;
end;

procedure TForm1.AfterPaintGIF(Sender: TObject);
begin
  if not (Sender is TGIFPainter) then Exit;
  if not Assigned(FBitmap) then Exit;
  // The event will ignore Empty frames      
  FBitmap.Canvas.Lock;
  try
    FBitmap.SaveToFile(Format('%.2d.bmp', [TGIFPainter(Sender).ActiveImage]));
  finally
    FBitmap.Canvas.Unlock;
  end;
  ProgressBar1.StepIt;
end;

注意:没有错误处理来简化代码。

output bitmaps

<小时/>

对于较新的 Delphi 版本 (2009+): 通过内置的 GIFImg 单元,您可以使用 TGIFRenderer 轻松完成此操作。 (完全取代了旧的 TGIFPainter)例如:

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

使用 GDI+:

uses ..., GDIPAPI, GDIPOBJ, GDIPUTIL;

procedure ExtractGifFrames(const FileName: string);
var
  GPImage: TGPImage;
  encoderClsid: TGUID;
  BmpFrame: TBitmap;
  MemStream: TMemoryStream;
  FrameCount, FrameIndex: Integer;
begin
  GPImage := TGPImage.Create(FileName);
  try
    if GPImage.GetLastStatus = Ok then
    begin
      GetEncoderClsid('image/bmp', encoderClsid);
      FrameCount := GPImage.GetFrameCount(GDIPAPI.FrameDimensionTime);
      for FrameIndex := 0 to FrameCount - 1 do
      begin
        GPImage.SelectActiveFrame(GDIPAPI.FrameDimensionTime, FrameIndex);
        MemStream := TMemoryStream.Create;
        try
          if GPImage.Save(TStreamAdapter.Create(MemStream), encoderClsid) = Ok then
          begin
            MemStream.Position := 0;
            BmpFrame := TBitmap.Create;
            try
              BmpFrame.LoadFromStream(MemStream);
              BmpFrame.SaveToFile(Format('%.2d.bmp', [FrameIndex]));
            finally
              BmpFrame.Free;
            end;
          end;
        finally
          MemStream.Free;
        end;
      end;
    end;
  finally
    GPImage.Free;
  end;
end;

关于delphi - TGifImage 透明度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10349050/

相关文章:

delphi - 目标多字节代码页中不存在 Unicode 字符的映射? (德尔福XE6)

delphi - TPrintDialog 和阅读方向参数

德尔福6 : How to create a Bitmap with TextOut that has an Alpha channel?

delphi - "Control ' ' has no parent window"错误

delphi - 非大小限制流函数的术语是什么?

delphi - 如何获取表单上的光标位置?

delphi - 使用 poPropagateChanges 和 poFetchDetailsOnDemand 避免 ClientDataSets 中的内存损坏?

compiler-errors - 德尔福 XE 2010 : Access Violation - How to remove package or component?

delphi - 使用 Unicode 的 AD 身份验证

delphi - 使用 TortoiseSVN 越过一垒