gpt4 book ai didi

delphi - 在Firemonkey上,如何在 Canvas 上绘制蒙版位图?

转载 作者:行者123 更新时间:2023-12-01 20:16:55 25 4
gpt4 key购买 nike

我有一个位图和一个蒙版(还有一个位图)。我想在蒙版上绘制位图(如下图所示)

drawing bitmap on a mask

如何在 Delphi 上使用 Firemonkey 执行此操作?

最佳答案

使用TBitmap.CreateFromBitmapAndMask()

constructor CreateFromBitmapAndMask(const Bitmap, Mask: TBitmap);

文档说:

The created TBitmap has the value of the Alpha channel of each color pixel equal with the value of the Red channel in the Mask.

进一步:

Tip: For a better result, use a grayscale image for Mask. It has an equal amount of green, red, and blue.

Tip: The mask and the base bitmap must have the same dimensions. Otherwise the new TBitmap will have the dimensions equal to 0.

在一个简单的测试中,例如:

procedure TForm19.Button1Click(Sender: TObject);
var
bmp, msk: TBitmap;
begin
bmp := nil;
msk := nil;
try
bmp := TBitmap.Create;
msk := TBitmap.Create;
bmp.LoadFromFile('C:\tmp\Imgs\4.bmp');
msk.LoadFromFile('C:\tmp\Imgs\TestImage04.bmp');
Image1.Bitmap := bmp;
Image2.Bitmap := msk;
Image3.Bitmap.CreateFromBitmapAndMask(bmp, msk);
finally
bmp.Free;
msk.Free;
end;
end;

结果如下所示:

enter image description here

编辑

为了使 CreateFromBitmapAndMask(bmp, msk); 的结果透明地绘制在表单上,​​在分配给 Image3< 之前必须对其进行预乘/。我们需要以下过程,

procedure PremultiplyBitmapAlpha(bmp:TBitmap);
var
X, Y: Integer;
M: TBitmapData;
C: PAlphaColorRec;
begin
if bmp.Map(TMapAccess.ReadWrite, M) then
try
for Y := 0 to bmp.Height - 1 do
for X := 0 to bmp.Width - 1 do
begin
C := @PAlphaColorArray(M.Data)[Y * (M.Pitch div 4) + X];
C^.Color := PremultiplyAlpha(C^.Color);
end;
finally
bmp.Unmap(M);
end;
end;

和另一个用于此目的的临时位图res。测试代码现在如下所示:

procedure TForm14.Button1Click(Sender: TObject);
var
bmp, msk, res: TBitmap;
begin
bmp := nil;
msk := nil;
res := nil;
try
bmp := TBitmap.Create;
msk := TBitmap.Create;
bmp.LoadFromFile('C:\tmp\Imgs\4.bmp');
msk.LoadFromFile('C:\tmp\Imgs\TestImage04.bmp');

Image1.Bitmap := bmp;
Image2.Bitmap := msk;

res := TBitmap.Create;
res.CreateFromBitmapAndMask(bmp, msk);

PremultiplyBitmapAlpha(res);
Image3.Bitmap := res;
finally
bmp.Free;
msk.Free;
res.Free;
end;
end;

图像(为了更好的演示而修改了背景颜色):

enter image description here

关于delphi - 在Firemonkey上,如何在 Canvas 上绘制蒙版位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46241917/

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