繁体   English   中英

在TPNGImage上拉伸

[英]Stretchdraw on TPNGImage

当我使用位图调用canvas.stretchdraw时,从左到右的位图将被镜像/翻转。 对于PNG,这不会发生。 这是错误吗? 我该如何解决?

要复制,请尝试以下代码:

procedure TForm1.TestStretchdraw;
var
  vBMP: TBitmap;
  vPNG: TPNGImage;
  X0,Y0,X1,Y1 : integer;
  R : TRect;

  procedure FlipRect;
  var
    T : integer;
  begin
    T := R.Left;
    R.Left := R.Right;
    R.Right := T;
  end;

begin
  vBMP := TBitmap.Create;
  vPNG := TPNGImage.Create;
  try
    vBMP.LoadFromFile('c:\temp\pic\pic.bmp');
    vPNG.LoadFromFile('c:\temp\pic\pic.png');

    X0 := 0;
    Y0 := 0;
    X1 := X0 + vBMP.Width;
    Y1 := Y0 + vBMP.Height;
    R := Rect(X0,Y0,X1,Y1);
    FlipRect;
    Canvas.StretchDraw(R,vBMP); //This image will be drawn mirrored

    X0 := vBMP.Width+10;
    Y0 := 0;
    X1 := X0 + vPNG.Width;
    Y1 := Y0 + vPNG.Height;
    R := Rect(X0,Y0,X1,Y1);
    FlipRect;
    Canvas.StretchDraw(R,vPNG); //This will not
  finally
    vPNG.Free;
    vBMP.Free;
  end;
end;

(但是用自己的一些替换我的testimages)

在这里,我编写了一个函数,该函数无需任何类似Gr32的库即可翻转png。 透明度保留在翻转的PNG中。

procedure FlipPNG(aSource, aDest: TPngImage);
var
  X, Y: Integer;
  AlphaPtr: PByteArray;
  RGBLine: pRGBLine;
  PalleteLine: PByteArray;
  AlphaPtrDest: PByteArray;
  RGBLineDest: pRGBLine;
  PalleteLineDest: PByteArray;
begin
  aDest.Assign(aSource);

  if (aSource.Header.ColorType = COLOR_PALETTE) or
     (aSource.Header.ColorType = COLOR_GRAYSCALEALPHA) or
     (aSource.Header.ColorType = COLOR_GRAYSCALE) then
  begin
    for y := 0 to aSource.Height - 1 do
    begin
      AlphaPtr := aSource.AlphaScanline[y];
      PalleteLine := aSource.Scanline[y];
      AlphaPtrDest := aDest.AlphaScanline[y];
      PalleteLineDest := aDest.Scanline[y];
      for x := 0 to aSource.Width - 1 do
      begin
        PalleteLineDest^[aSource.Width - x -1] := PalleteLine^[x];
        if Assigned(AlphaPtr) then
          AlphaPtrDest^[aSource.Width - x -1] := AlphaPtr^[x];
      end;
    end;
  end else
  if (aSource.Header.ColorType = COLOR_RGBALPHA) or
     (aSource.Header.ColorType = COLOR_RGB) then
  begin
    for y := 0 to aSource.Height - 1 do
    begin
      AlphaPtr := aSource.AlphaScanline[y];
      RGBLine := aSource.Scanline[y];
      AlphaPtrDest := aDest.AlphaScanline[y];
      RGBLineDest := aDest.Scanline[y];
      for x := 0 to aSource.Width - 1 do
      begin
        RGBLineDest^[aSource.Width - x -1] := RGBLine^[x];
        if Assigned(AlphaPtr) then
          AlphaPtrDest^[aSource.Width - x -1] := AlphaPtr^[x];
      end;
    end;
  end;
end;

是的,这是正确的。 StretchDraw调用TGraphic的适当后代的Draw方法。 您可以自己比较TBitmap.DrawTPngImage.Draw TBitmap.Draw自然会简单地调用Windows API函数StretchBlt TPngImage.Draw但是,首先调用AdjustRect

procedure AdjustRect(var Rect: TRect);
var
  t: Integer;
begin
  if Rect.Right < Rect.Left then
  begin
    t := Rect.Right;
    Rect.Right := Rect.Left;
    Rect.Left := t;
  end;
  if Rect.Bottom < Rect.Top then
  begin
    t := Rect.Bottom;
    Rect.Bottom := Rect.Top;
    Rect.Top := t;
  end
end;

如您所见,它无情地撤消了您对leftright交换。

我不会考虑这样的情况,其中Left大于Right ,这是一个有效的矩形定义,所以我的答案是:不要那样称呼它。 您可以在调用StretchDraw之前确保矩形定义有效:

if (R.Left > R.Right) then
  Swap (R.Left, R.Right);
if (R.Top> R.Bottom) then
  Swap (R.Top, R.Bottom);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM