gpt4 book ai didi

delphi - 在 TPNGImage 上拉伸(stretch)绘制

转载 作者:行者123 更新时间:2023-12-01 19:29:38 28 4
gpt4 key购买 nike

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

(但用你自己的一些测试图像替换我的测试图像)

最佳答案

这里我编写了一个翻转 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;

关于delphi - 在 TPNGImage 上拉伸(stretch)绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9975915/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com