简体   繁体   English

TBitmap在Delphi 2009中绘制透明图像

[英]TBitmap drawing transparent image in Delphi 2009

Problem in drawing a semi transparent PNG image on TBitmap object. 在TBitmap对象上绘制半透明PNG图像时出现问题。

If the TBitmap's ,HandleType is set to bmDDB, then the canvas is drawn transparent. 如果TBitmap的HandleType设置为bmDDB,则画布将绘制为透明。 But the problem is it doesn't work on all kinds of machines (for ex: Windows on apple computers). 但问题是它不适用于所有类型的机器(例如:苹果计算机上的Windows)。

When a TBitmap's HandleType property is set to bmDIB, canvas background is drawn white. 当TBitmap的HandleType属性设置为bmDIB时,画布背景将绘制为白色。

bmp.HandleType := bmDIB;

I tried setting Brush style to bsClear. 我尝试将Brush样式设置为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: 当然可以将具有透明背景的bmDIB位图绘制到画布:

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 . 请注意,首先填充整个位图,颜色设置为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 . 但是为了获得更多的控制和速度,你应该研究一种不依赖于GDI(涉及图形卡和驱动程序功能)的解决方案,比如Graphics32

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM