Author Topic: [SOLVED] How to draw on canvas with semi-transparency?  (Read 785 times)

artem101

  • Full Member
  • ***
  • Posts: 127
[SOLVED] How to draw on canvas with semi-transparency?
« on: August 17, 2026, 09:34:57 am »
Task is very simple - draw red circle on TImage canvas with 50% transparency fill.

I tried to set different values (0, $ffff, $ffff div 2) to Brush.FPColor alpha, but result is the same - no transparency.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   with Image1.Canvas do
  4.   begin
  5.     DrawingMode := dmAlphaBlend; // no any effect with/without this
  6.  
  7.     Brush.Color:=clBlack;
  8.     Pen.Style:=psClear;
  9.     Clear;
  10.  
  11.     Brush.Color:=clGreen;
  12.     Ellipse(50, 50, 150, 150);
  13.  
  14.     // How to draw second circle with 50% transparency?
  15.     //Brush.Color:=clRed;
  16.     Brush.FPColor:=FPColor($ffff,0000,0000, $ffff div 2);
  17.     Ellipse(80, 80, 200, 200);
  18.  
  19.   end;
  20. end;  

How to make this?

Thanks.
« Last Edit: August 17, 2026, 12:20:31 pm by artem101 »

paweld

  • Hero Member
  • *****
  • Posts: 1734
Best regards / Pozdrawiam
paweld

ADMGNS

  • Jr. Member
  • **
  • Posts: 90
  • keep it simple and smart
Re: How to draw on canvas with semi-transparency?
« Reply #2 on: August 17, 2026, 11:19:15 am »
hello

with RGBABitmap:

Code: Pascal  [Select][+][-]
  1. uses
  2. ..., BGRABitmap, BGRABitmapTypes;
  3.  
  4.  
  5. procedure TForm1.FormPaint(Sender: TObject);
  6. var
  7.   Bmp: TBGRABitmap;
  8. begin
  9.   Bmp := TBGRABitmap.Create(256, 256, BGRA(240, 240, 240, 128)); // no transparency effect
  10.   try
  11.     Bmp.FillRect(50, 50, 200, 200, BGRA(255, 0, 0, 128));
  12.  
  13.     // together, use same color, 150 is transparency in RGBA()
  14.     Bmp.FillEllipseAntialias(100, 100, 80, 80, BGRA(0, 255, 0, 150));
  15.     Bmp.EllipseAntialias(100, 100, 80, 80, BGRA(0, 255, 0, 150), 1);
  16.  
  17.     Bmp.Draw(Canvas, (ClientWidth-bmp.Width) div 2, (ClientHeight-bmp.Height) div 2, True);
  18.   finally
  19.     Bmp.Free;
  20.   end;
  21. end;
  22.  

artem101

  • Full Member
  • ***
  • Posts: 127
Re: How to draw on canvas with semi-transparency?
« Reply #3 on: August 17, 2026, 12:19:48 pm »
Thank you!

Result:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   Bmp: TBGRABitmap;
  4. begin
  5.   Bmp := TBGRABitmap.Create(Image1.Width, Image1.Height, BGRA(0,0,0));
  6.   try
  7.     bmp.FillEllipseInRect(Rect(50, 50, 150, 150), BGRA(0,255,0));
  8.     bmp.FillEllipseInRect(Rect(80, 80, 200, 200), BGRA(255,0,0,255 div 2));
  9.  
  10.     Bmp.Draw(Image1.Canvas, (ClientWidth-bmp.Width) div 2, (ClientHeight-bmp.Height) div 2, True);
  11.   finally
  12.     Bmp.Free;
  13.   end;
  14. end;    

 

TinyPortal © 2005-2018