TPNGImage上的Stretchdraw

时间:2012-04-02 11:54:10

标签: delphi graphics png

当我使用位图调用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)

3 个答案:

答案 0 :(得分:5)

在这里,我编写了一个函数,它可以翻转png而不需要任何像Gr32这样的库。透明度仍然在翻转的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;

答案 1 :(得分:3)

是的,这是正确的。 StretchDraw调用Draw的适当后代的TGraphic方法。您可以自己将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的交换。

答案 2 :(得分:0)

我不会考虑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);