TBitmap在Delphi 2009中绘制透明图像 [英] TBitmap drawing transparent image in Delphi 2009

查看:1490
本文介绍了TBitmap在Delphi 2009中绘制透明图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果将TBitmap的HandleType设置为bmDDB,则画布将被透明化。
但问题是它不适用于各种机器(例如苹果计算机上的Windows)。



当设置TBitmap的HandleType属性时到bmDIB,画布背景是白色的。

  bmp.HandleType:= bmDIB; 

我尝试将Brush样式设置为bsClear。但是它绘制了黑色的透明像素。



如何绘制一幅保持透明度和平滑曲线边缘的图像。



感谢
Pavan。

解决方案

当然可以绘制一个 bmDIB 位图与透明背景画布:

 程序TForm1.FormPaint(发件人:TObject); 
var
Bmp:TBitmap;
begin
Bmp:= TBitmap.Create;
try
Bmp.PixelFormat:= pf32bit;
Bmp.HandleType:= bmDIB;
Bmp.Width:= 700;
Bmp.Height:= 400;
Bmp.Transparent:= TRUE;
Bmp.TransparentColor:= clMaroon;

with Bmp.Canvas do begin
Brush.Color:= clMaroon;
FillRect(Rect(0,0,Bmp.Width,Bmp.Height));

Brush.Color:= clBlue;
FillRect(Rect(42,42,200,300));
结束

Canvas.Draw(12,12,Bmp);
finally
Bmp.Free;
结束
结束

请注意,整个位图首先填充颜色设置为 TransparentColor



但是,为了更多的控制和速度,您应该研究一种不依赖于GDI的解决方案(涉及显卡和驱动程序功能),例如 Graphics32


Problem in drawing a semi transparent PNG image on TBitmap object.

If the TBitmap's ,HandleType is set to bmDDB, then the canvas is drawn transparent. But the problem is it doesn't work on all kinds of machines (for ex: Windows on apple computers).

When a TBitmap's HandleType property is set to bmDIB, canvas background is drawn white.

bmp.HandleType := bmDIB;

I tried setting Brush style to bsClear. But it draws the transparent pixels in black color.

How can I draw an image preserving its transparency and smooth curved edges.

Thanks Pavan.

解决方案

It is certainly possible to paint a bmDIB bitmap with transparent background to a canvas:

procedure TForm1.FormPaint(Sender: TObject);
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.PixelFormat := pf32bit;
    Bmp.HandleType := bmDIB;
    Bmp.Width := 700;
    Bmp.Height := 400;
    Bmp.Transparent := TRUE;
    Bmp.TransparentColor := clMaroon;

    with Bmp.Canvas do begin
      Brush.Color := clMaroon;
      FillRect(Rect(0, 0, Bmp.Width, Bmp.Height));

      Brush.Color := clBlue;
      FillRect(Rect(42, 42, 200, 300));
    end;

    Canvas.Draw(12, 12, Bmp);
  finally
    Bmp.Free;
  end;
end;

Note that the whole bitmap is filled first with the colour set as TransparentColor.

But for more control and speed you should look into a solution that is not as dependent on the GDI (which involves graphics card and driver capabilities), something like Graphics32.

这篇关于TBitmap在Delphi 2009中绘制透明图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆