Author Topic: [Solved] Need help understanding BGRABitmap "Draw"  (Read 349 times)


  • Newbie
  • Posts: 2
[Solved] Need help understanding BGRABitmap "Draw"
« on: December 17, 2020, 09:18:37 pm »
Hi everyone,

I'm completely new to Lazarus/Free Pascal, but I have some programming knowledge. I'm trying to understand how to draw with BGRABitmap.

I created some GUI (see attached image) and added an event handler for the OnRedraw event of the BGRAVirtualscreen. Then I just copied some code from a tutorial to draw something:

Code: Pascal  [Select][+][-]
  1. procedure TFormMain.BGRAVirtualScreenMainRedraw(Sender: TObject; Bitmap: TBGRABitmap);
  2. begin
  3.   Bitmap.FillRect(20, 20, 100, 40, BGRA(255, 192, 0), dmSet);
  4.   Bitmap.Draw(Canvas, 0, 0, True);
  5. end;

All of this is working as expected. What I don't understand is the code with "Bitmap.Draw": Where is "Canvas" defined? This line of code can be found in lots of tutorials, but I never found an explanation for it. My TFormMain is inherited from class TForm, maybe TForm has a property "Canvas". But then I don't understand why it's correctly drawing on the virtual screen and not on the MainForm... this is really confusing to me. Also, why does Lazarus add the event handler as part of the TFormMain and not as part of BGRAVirtualScreenMain?
« Last Edit: December 28, 2020, 08:41:44 pm by quotienscumque »


  • Hero Member
  • *****
  • Posts: 2219
Re: Need help understanding BGRABitmap "Draw"
« Reply #1 on: December 17, 2020, 09:36:25 pm »

Lazarus basics:

If no other owner of the canvas is specified then it is the class of the  calling function or procedure.
As the procedure TFormMain...... is invoked the canvas is the canvas of TFormMain.



  • Hero Member
  • *****
  • Posts: 3965
  • Leandro Diaz
Re: Need help understanding BGRABitmap "Draw"
« Reply #2 on: December 17, 2020, 11:02:57 pm »
Seems that the tutorial is wrong, since there is no need to draw the bitmap on canvas, is done automatically by the virtual screen control.

The only thing you need to do is draw something with the Bitmap and nothing more. - My Website :) -  Download LazPaint


  • Newbie
  • Posts: 2
Re: Need help understanding BGRABitmap "Draw"
« Reply #3 on: December 28, 2020, 08:40:49 pm »
Hi guys,

sorry for the late answer. Your answers helped a lot, and after reading lainz' answer I just removed the code which draws on "Canvas". It still works, so looks like the tutorial was wrong. Thanks again for the help!  :)


TinyPortal © 2005-2018