【发布时间】:2012-03-19 09:40:42
【问题描述】:
给定一个包含一些 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;
【问题讨论】:
-
不存在真正的一般情况,因为并非所有 TGraphic 后代都支持部分 (>1bit) 透明度。
-
是的,32 位位图有一个 Alpha 通道。请参阅下面的答案。