Recent

Author Topic: [SOLVED] Trouble getting a TBitmap to display on a TPanel.  (Read 946 times)

indydev

  • Full Member
  • ***
  • Posts: 127
[SOLVED] Trouble getting a TBitmap to display on a TPanel.
« on: July 25, 2023, 06:23:53 pm »
When I put the following code into the FormCreate event, the Bitmap does not display.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.    AgentPic: TBitmap;
  4.    PicRect: TRect;
  5. begin
  6.     AgentPic := TBitmap.Create;
  7.   try
  8.     AgentPic.LoadFromFile('Agent.bmp');
  9.     Panel4.Canvas.Brush.Color := Panel4.Color;
  10.     Panel4.Canvas.FillRect(Panel4.ClientRect);
  11.     PicRect := Rect(24, 8, 54, 38);
  12.  
  13.     Panel4.Canvas.StretchDraw(PicRect, AgentPic);
  14.   finally
  15.     AgentPic.Free;
  16.   end;
  17. end;    

If I put the same code in the FormResize event, and then 'resize' the form, the bitmap displays. The same code in onShow, or onRepaint does not display (onResize removed). If I leave the code in FormResize(Sender);  and call it from FormCreate(Sender); it does not display.

I am confused as to why the bitmap does not display in FormCreate, or FormShow, or FormPaint (assigned to their respective 'on' events);

If someone knows what is actually triggering the display in the FormResize(Sender); procedure so I can get this bitmap displayed without having to resize the form, I would appreciate your help.

Thanks.
« Last Edit: July 25, 2023, 08:20:03 pm by indydev »

Handoko

  • Hero Member
  • *****
  • Posts: 5416
  • My goal: build my own game engine using Lazarus
Re: Trouble getting a TBitmap to display on a TPanel.
« Reply #1 on: July 25, 2023, 07:06:47 pm »
OnCreate event is for your program to initialize data. If you do any drawing/painting here, nothing will be drawn on the screen, and it may cause exception. The reason is, The form, canvas, etc aren't actually being created when the execution runs into the OnCreate event. As the name suggests, it is for you to initialize your data.

OnRepaint? I checked my TForm, I couldn't find OnRepaint. No comment.

OnResize, as the name suggests it will be fired when you resize the form. Put you code here if you need to do the calculation for rearranging the layout if the size of the form is changed.

OnPaint. Any drawing/painting should be put here. As the name suggests OnPaint, any non-painting related code shouldn't put here. For your case, loading an image from disk, shouldn't be here. You may not notice the problem, but if the file is huge and you're running the code on a slow computer, the program will be noticeable delay. The better solution is load the picture only once and store in the memory, so there will be no reloading next time you need it.

OnShow. Don't put any drawing/painting code here. I rarely use it, no comment.
« Last Edit: July 25, 2023, 07:08:35 pm by Handoko »

Josh

  • Hero Member
  • *****
  • Posts: 1372
Re: Trouble getting a TBitmap to display on a TPanel.
« Reply #2 on: July 25, 2023, 08:05:00 pm »
Hi

any component drawing should be done in  components onpaint event, as you cant guarantee if component is ready for painting/updating any other way. Also the LCL will decide when the component needs repaint; and then your code gets run also. You can call onpaint youself, but do so only when you need to, it is possible you can cause an infinate loop if done incorrectly.
project demo attached, not the neatest but should work.


The best way to get accurate information on the forum is to post something wrong and wait for corrections.

indydev

  • Full Member
  • ***
  • Posts: 127
Re: Trouble getting a TBitmap to display on a TPanel.
« Reply #3 on: July 25, 2023, 08:19:38 pm »
Ok, figured it out. My slip up here. I was calling the code to display from the TForm and not the TPanel itself. Thank you both for responding. Handoko's explanation made me be more methodical about my Bitmap, but Josh's code in the Panel's own paint method made me realize I was trying to call from the Form's paint method (which I thought would work, but obviously doesn't), when I should call from the Panel's paint method.

great catch!

 

TinyPortal © 2005-2018