簡體   English   中英

如何將每像素alpha的位圖繪制到控件的Canvas上?

[英]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參數是目標畫布, XY是坐標,其中位圖將在目標畫布中呈現, 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM