创建透明的自定义位图画笔

时间:2015-07-17 01:20:01

标签: delphi delphi-xe7

问题定义

我正在尝试创建一个具有透明度的自定义位图画笔,但它似乎没有按预期工作。如果你看一下这个例子。添加代码并连接绘制,创建和销毁事件。

type
  TForm3 = class(TForm)
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
    FBitmap: TBitmap;
  end;

// Implementation

function CreateBlockBitmap(const APenColor: TColor): TBitmap;
begin
  Result := TBitmap.Create;
  Result.Transparent := True; 
  Result.Canvas.Brush.Color := clWhite;
  Result.Canvas.Brush.Style := bsClear;
  Result.PixelFormat := pf32bit;
  Result.SetSize(20, 20);
  Result.Canvas.Brush.Color := APenColor;
  Result.Canvas.Brush.Style := bsSolid;
  Result.Canvas.FillRect(Rect(0,0,10,10));
end;

procedure TForm3.FormDestroy(Sender: TObject);
begin
  FBitmap.Free;
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  FBitmap := CreateBlockBitmap(clRed);
end;

procedure TForm3.FormPaint(Sender: TObject);
var
  colNum: Integer;
  rowNum: Integer;
begin
  // Paint the rectangle using the brush
  Canvas.Pen.Color := clGreen;
  Canvas.Brush.Bitmap := FBitmap; // This is using bitmap
  Canvas.Rectangle(50, 50, 250, 250);
  // Draw the block using Canvas.Draw
  for rowNum := 0 to 9 do
    for colNum := 0 to 9 do
      Canvas.Draw(350 + rowNum * 20, 50 + colNum * 20, FBitmap);
end;

此代码生成两个绘制的块。左边的一个使用位图画笔绘制,右边的一个使用多个Canvas.Draw调用绘制。

Brush Transparency

我需要刷涂上透明度,类似于使用阴影刷时会发生的情况。这个答案似乎表明它是可能的:

How can I draw a patternBrush with transparent backround (GDI)?

我尝试了什么

1)我尝试使用纯色背景而不是bsClear。这只会使背景变白。

  Result.Canvas.Brush.Color := clWhite;
  Result.Canvas.Brush.Style := bsSolid;

如果我使用clFuchsia,那么颜色就是紫红色。我还尝试绘制背景clFuchsia,然后将TransparentColor设置为clFuchsiaCanvas.Draw选项使用透明度绘制,而画笔不绘制。

2)我尝试使用以下代码直接设置alpha通道:

procedure SetAlphaBitmap(const Dest: TBitmap;Color : TColor;Alpha:Byte);
type
  TRGB32 = record
    B, G, R, A: byte;
  end;
  PRGBArray32 = ^TRGBArray32;
  TRGBArray32 = array[0..0] of TRGB32;
var
  x, y:    integer;
  Line, Delta: integer;
  ColorRGB : TColor;
begin
  if Dest.PixelFormat<>pf32bit then  exit;

  ColorRGB := ColorToRGB(Color);
  Line  := integer(Dest.ScanLine[0]);
  Delta := integer(Dest.ScanLine[1]) - Line;
  for y := 0 to Dest.Height - 1 do
  begin
    for x := 0 to Dest.Width - 1 do
      if TColor(RGB(PRGBArray32(Line)[x].R, PRGBArray32(Line)[x].G, PRGBArray32(Line)[x].B)) = ColorRGB then
        PRGBArray32(Line)[x].A := Alpha;
    Inc(Line, Delta);
  end;
end;

然后在使用背景颜色绘制矩形后立即调用此例程。

  Result.Canvas.Brush.Style := bsSolid;
  Result.Canvas.FillRect(Rect(0,0,10,10));
  SetAlphaBitmap(Result, clBlack, 0); // Set the alpha channel
end;

我知道alpha通道正常工作,因为如果我传入的alpha值为255,那么它在Canvas.Draw中也显示为黑色。

  SetAlphaBitmap(Result, clBlack, 255);

3)我尝试通过创建模式画笔并分配而不是位图来进行测试。这产生了完全相同的结果。 FBrush是HBRUSH。

  FBrush := CreatePatternBrush(FBitmap.Handle);

设置画笔如下:

  Canvas.Brush.Handle := FBrush; 

4)我尝试按照上面的答案中的说明调用SetBkMode。这没有任何区别。

  Canvas.Pen.Color := clGreen;
  Canvas.Brush.Bitmap := FBitmap; 
  SetBkMode(Canvas.Handle, TRANSPARENT); // This doesn't make a difference
  Canvas.Rectangle(50, 50, 250, 250);

修改

5)我刚用单色位图进行测试,它有同样的问题。图像使用白色背景和画笔的黑色前景绘制,并为Canvas.Draw透明。

function CreateMonochromeBitmap: TBitmap;
begin
  Result := TBitmap.Create;
  Result.Transparent := True;
  Result.Canvas.Brush.Color := clWhite;
  Result.Canvas.Brush.Style := bsSolid;
  Result.PixelFormat := pf1bit;
  Result.SetSize(20, 20);
  Result.Canvas.Brush.Color := clBlack;
  Result.Canvas.Brush.Style := bsSolid;
  Result.Canvas.FillRect(Rect(0,0,10,10));
end;

在构造函数中:

FBitmap := CreateMonochromeBitmap;
FBrush := CreatePatternBrush(FBitmap.Handle);

在绘画中我们设置句柄而不是位图属性。

Canvas.Brush.Handle := FBrush; 

3 个答案:

答案 0 :(得分:1)

尝试在绘制循环之前清除画布此null颜色。

Canvas.Clear(TAlphaColorRec.Null);

问候。 保罗。

答案 1 :(得分:0)

您需要将白色用于透明区域,并在填充矩形之前先使用SetROP2,如下所示:

Canvas.Brush.Bitmap := FBitmap; // This is using bitmap
SetROP2(Canvas.Handle, R2_MASKPEN); 
Canvas.Rectangle(50, 50, 250, 250);

别忘了恢复以前的ROP模式。

祝你好运!

答案 2 :(得分:0)

解决了!这是我的解决方案:

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  public
    FBitmap: TBitmap;
  end;

//Implementation

function CreateBlockBitmap: TBitmap;
begin
  Result := TBitmap.Create;
  Result.PixelFormat := pf1bit;  //!! 1-bit
  Result.Width := 20;
  Result.Height := 20;
  Result.Canvas.Brush.Color := clBlack;
  Result.Canvas.FillRect(Rect(0, 0, 10, 10));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FBitmap := CreateBlockBitmap;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FBitmap.Free;
end;

procedure TForm1.FormPaint(Sender: TObject);
const
  PatternColor = clRed;   //brush color to be used
var
  R: TRect;
begin
  //filling the background with different colors for test 
  Canvas.Brush.Color := clGreen;
  Canvas.FillRect(Rect(0,0,100,600));
  Canvas.Brush.Color := clAqua;
  Canvas.FillRect(Rect(100,0,200,600));
  Canvas.Brush.Color := clYellow;
  Canvas.FillRect(Rect(200,0,300,600));
  Canvas.Brush.Color := clWhite;
  Canvas.FillRect(Rect(300,0,400,600));

  //draw the rectangle
  R := Rect(50, 50, 500, 500);
  Canvas.Brush.Color := PatternColor;
  BitBlt(Canvas.Handle, R.Left, R.Top, R.Right, R.Bottom, Canvas.Handle, 0, 0, PATINVERT);
  Canvas.Brush.Bitmap := FBitmap;
  SetROP2(Canvas.Handle, R2_MASKPEN);

  Canvas.Rectangle(R);  //draw any figure here

  Canvas.Brush.Color := PatternColor;
  SetROP2(Canvas.Handle, R2_COPYPEN);
  BitBlt(Canvas.Handle, R.Left, R.Top, R.Right, R.Bottom, Canvas.Handle, 0, 0, PATINVERT);
end;