gpt4 book ai didi

delphi - 需要计算哪些像素是(完全)透明的

转载 作者:行者123 更新时间:2023-12-03 15:22:31 29 4
gpt4 key购买 nike

给定一个包含一些 TGraphic 后代的 Delphi TPicture,我需要计算像素颜色和不透明度。我认为我必须为每个类提供不同的实现,并且我认为我已经涵盖了 TPngImage。 32 位位图是否支持透明度?我可以用比以下更通用的方式解决这个问题吗?:

procedure GetPixelColorAndTransparency(const Picture: TPicture; X, Y:
Integer; out Color: TColor; out Opacity: Byte);
var
Bmp: TBitmap;
begin
if Picture.Graphic is TPngImage then
begin
Opacity := (TPngImage(Picture.Graphic).AlphaScanline[Y]^)[X];
Color := TPngImage(Picture.Graphic).Pixels[ X, Y ];
end
else
if Picture.Graphic is TBitmap then
begin
Color := Picture.Bitmap.Canvas.Pixels[ X, Y ];
Opacity := 255;
end
else
begin
Bmp := TBitmap.Create;
try
Bmp.Assign(Picture.Graphic);
Color := Bmp.Canvas.Pixels[ X, Y ];
Opacity := 255;
finally
Bmp.Free;
end;
end;
end;

最佳答案

像这样怎么样:

procedure GetPixelColorAndTransparency(const Picture: TPicture; X, Y: Integer; out Color: TColor; out Opacity: Byte); 
type
PRGBQuadArray = ^TRGBQuadArray;
TRGBQuadArray = array [Integer] of TRGBQuad;
var
Bmp: TBitmap;
begin
if Picture.Graphic is TPngImage then
begin
with TPngImage(Picture.Graphic) do begin
Opacity := AlphaScanline[Y]^[X];
Color := Pixels[X, Y];
end;
end
else if Picture.Graphic is TBitmap then
begin
with Picture.Bitmap do begin
Color := Canvas.Pixels[X, Y];
if PixelFormat = pf32Bit then begin
Opacity := PRGBQuadArray(Scanline[Y])^[X].rgbReserved;
end
else if Color = TranparentColor then begin
Opacity := 0;
end
else begin
Opacity := 255;
end;
end;
end else
begin
Bmp := TBitmap.Create;
try
Bmp.Assign(Picture.Graphic);
Color := Bmp.Canvas.Pixels[X, Y];
if Color = Bmp.TranparentColor then begin
Opacity := 0;
end else begin
Opacity := 255;
end;
finally
Bmp.Free;
end;
end;
end;

关于delphi - 需要计算哪些像素是(完全)透明的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9506987/

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