1

我正在尝试TGifimage使用以下过程调整动画大小。我可以毫无问题地调整大小,但动画 gif 的质量非常差。

知道如何提高质量吗?

目前,动画 gif 显示为黑色,看起来已损坏。

procedure ResizeGif(Src, Dst: TGifImage; const newHeight, newWidth: integer);
var
  bmp, bmp2: TBitmap;
  gifren: TGIFRenderer;
  I: integer;

begin
  if (Src.Empty) or not assigned(Src.Images[0]) then
  begin
    exit;
  end;

  bmp := TBitmap.Create;
  bmp2 := TBitmap.Create;
  gifren := TGIFRenderer.Create(Src);

  try
    bmp.PixelFormat := pf24bit;
    bmp2.PixelFormat := pf24bit;
    bmp.Width := newWidth;
    bmp.Height := newHeight;

    for I := 0 to Src.Images.Count - 1 do
    begin

      bmp.SetSize(newWidth, newHeight);

      gifren.Draw(bmp.Canvas, bmp.Canvas.ClipRect);

      bmp2.SetSize(newWidth, newHeight);

      bmp2.Canvas.StretchDraw(Rect(0, 0, newWidth, newHeight), bmp);

      TGIFGraphicControlExtension.Create(Dst.add(bmp2)).Delay :=
        gifren.FrameDelay div 10;;

      gifren.NextFrame;

    end;

    TGIFAppExtNSLoop.Create(Dst.Images.Frames[0]).Loops := 0;

  finally
    bmp.free;
    bmp2.free;
    gifren.free;
  end;

end;
4

1 回答 1

0

据我所知,您根本无法使用标准库来做到这一点,您必须使用例如Graphics32。然后你可以编写一个像下面这样的简单函数并选择采样器,这取决于你到底需要什么,请参阅Resampling

Class name        | Quality | Performance
------------------------------------------
TNearestResampler | low     | high
TDraftResampler   | medium  | high (downsampling only)
TLinearResampler  | medium  | medium
TKernelResampler  | high    | low (depends on kernel width)

示例TGraphicHelper.Resize

procedure TGraphicHelper.Resize(const AWidth, AHeight: Integer);
var
  lBmpSource, lBmpResize : TBitmap32;
  lBmpTemp : TBitmap;
begin
  lBmpSource := TBitmap32.Create;
  try
    TDraftResampler.Create(lBmpSource);
    lBmpSource.DrawMode := dmOpaque;
    lBmpSource.Assign(Self);

    lBmpResize := TBitmap32.Create;
    try
      lBmpResize.Width := AWidth;
      lBmpResize.Height := AHeight;
      lBmpResize.Clear(clWhite32);
      lBmpResize.Draw(lBmpResize.BoundsRect, lBmpSource.BoundsRect, lBmpSource);

      lBmpTemp := TBitmap.Create;
      try
        lBmpTemp.Assign(lBmpResize);
        Self.Assign(lBmpTemp);
      finally
        lBmpTemp.Free;
      end;
    finally
      lBmpResize.Free;
    end;
  finally
    lBmpSource.Free;
  end;
end;
于 2016-12-19T08:14:40.580 回答