gpt4 book ai didi

windows - 如何将具有每像素 alpha 的位图绘制到控件的 Canvas 上?

转载 作者:可可西里 更新时间:2023-11-01 13:54:50 25 4
gpt4 key购买 nike

问题

将具有逐像素 alpha 的位图绘制到控件的 Canvas 上的最佳方法是什么?

我的位图数据存储在 32 位像素值的二维数组中。

T32BitPixel = packed record
Blue : byte;
Green : byte;
Red : byte;
Alpha : byte;
end;

我的控件是 TCustomTransparentControl 的后代.

背景

对于我正在构建的 GUI,我需要在其他控件和纹理背景上绘制半透明控件。控制图形是使用 AggPasMod 创建的(Anti-Grain Geometry 的一个端口)。

TCustomTransparentControl.Canvas.Handle 提供对 device context 的访问用于绘图,但我不确定如何从那里 blit 像素数据。

最佳答案

假设您的像素阵列由图像行和其中的像素组成,我会这样做。 Canvas 参数是目标 Canvas ,XY 是坐标,位图将在目标 Canvas 中渲染,Pixels 是像素数组:

type
TPixel = packed record
B: Byte;
G: Byte;
R: Byte;
A: Byte;
end;
TPixelArray = array of array of TPixel;

procedure RenderBitmap(Canvas: TCanvas; X, Y: Integer; Pixels: TPixelArray);
var
I: Integer;
Size: Integer;
Bitmap: TBitmap;
BlendFunction: TBlendFunction;
begin
Bitmap := TBitmap.Create;
try
Bitmap.PixelFormat := pf32bit;
Bitmap.Width := Length(Pixels[0]);
Bitmap.Height := Length(Pixels);
Size := Bitmap.Width * SizeOf(TPixel);

for I := 0 to Bitmap.Height - 1 do
Move(Pixels[I][0], Bitmap.ScanLine[I]^, Size);

BlendFunction.BlendOp := AC_SRC_OVER;
BlendFunction.BlendFlags := 0;
BlendFunction.SourceConstantAlpha := 255;
BlendFunction.AlphaFormat := AC_SRC_ALPHA;
AlphaBlend(Canvas.Handle, X, Y, Bitmap.Width, Bitmap.Height,
Bitmap.Canvas.Handle, 0, 0, Bitmap.Width, Bitmap.Height, BlendFunction);
finally
Bitmap.Free;
end;
end;

关于windows - 如何将具有每像素 alpha 的位图绘制到控件的 Canvas 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13925061/

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