[SOLVED] Trouble getting a TBitmap to display on a TPanel. lazarus / 39-Graphics ---------------------------------------------------------------------- indydev · 2023-07-25 16:23 When I put the following code into the FormCreate event, the Bitmap does not display. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var   AgentPic: TBitmap;   PicRect: TRect;begin    AgentPic := TBitmap.Create;  try    AgentPic.LoadFromFile('Agent.bmp');    Panel4.Canvas.Brush.Color := Panel4.Color;    Panel4.Canvas.FillRect(Panel4.ClientRect);    PicRect := Rect(24, 8, 54, 38);     Panel4.Canvas.StretchDraw(PicRect, AgentPic);  finally    AgentPic.Free;  end;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. ---------------------------------------------------------------------- Handoko · 2023-07-25 17:06 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. ---------------------------------------------------------------------- Josh · 2023-07-25 18:05 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. ---------------------------------------------------------------------- indydev · 2023-07-25 18:19 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!