[英]How to draw a bitmap with per-pixel alpha onto a control's Canvas?
題
將每像素alpha位圖繪制到控件的畫布上的最佳方法是什么?
我的位圖數據存儲在32位像素值的2d數組中。
T32BitPixel = packed record
Blue : byte;
Green : byte;
Red : byte;
Alpha : byte;
end;
我的控制是TCustomTransparentControl的后代。
背景
對於我正在構建的GUI,我需要在其他控件和紋理背景上繪制半透明控件。 使用AggPasMod (Anti-Grain Geometry的一個端口)創建控制圖形。
TCustomTransparentControl.Canvas.Handle提供了對繪圖設備上下文的訪問,但我不知道如何從那里blit像素數據。
假設你的像素數組像圖像行和像素一樣,我會這樣做。 Canvas
參數是目標畫布, X
和Y
是坐標,其中位圖將在目標畫布中呈現, 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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.