TBitmap drawing transparent image in Delphi 2009

爷,独闯天下 提交于 2019-12-05 02:07:35

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1728141/tbitmap-drawing-transparent-image-in-delphi-2009

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!