简体   繁体   English

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

[英]How to draw a bitmap with per-pixel alpha onto a control's Canvas?

Question

What's the best way to draw a bitmap with a per-pixel alpha onto a control's Canvas? 将每像素alpha位图绘制到控件的画布上的最佳方法是什么?

My bitmap data is stored in a 2d array of 32-bit pixel values. 我的位图数据存储在32位像素值的2d数组中。

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

My control is a descendent of TCustomTransparentControl . 我的控制是TCustomTransparentControl的后代。

Background 背景

For a GUI I'm building I need to draw semi-transparent controls over other controls and textured backgrounds. 对于我正在构建的GUI,我需要在其他控件和纹理背景上绘制半透明控件。 The control graphics are created using AggPasMod (a port of Anti-Grain Geometry). 使用AggPasMod (Anti-Grain Geometry的一个端口)创建控制图形。

TCustomTransparentControl.Canvas.Handle provides access to the device context for drawing but I'm not sure how to blit the pixel data from there. TCustomTransparentControl.Canvas.Handle提供了对绘图设备上下文的访问,但我不知道如何从那里blit像素数据。

Assuming, you have your pixel array composed like the image rows and pixels in them, I would do it this way. 假设你的像素数组像图像行和像素一样,我会这样做。 The Canvas parameter is a target canvas, the X and Y are coordinates, where the bitmap will be rendered in the target canvas and the Pixels is the pixel array: 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