Recent

Author Topic: Using of TImage.Canvas  (Read 946 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 439
Using of TImage.Canvas
« on: February 24, 2026, 06:15:36 pm »
I need to draw on TImage. Load of an actual picture is not needed. What should I use TImage.Width or TImage.Picture.Width and what difference between this two?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Xenno

  • Jr. Member
  • **
  • Posts: 87
    • BS Programs
Re: Using of TImage.Canvas
« Reply #1 on: February 24, 2026, 08:06:45 pm »
TImage.Width : width of the TImage
TImage.Picture.Width : width of the loaded picture or original image dimension.

Why not use TPaintBox for the purpose?
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

LemonParty

  • Sr. Member
  • ****
  • Posts: 439
Re: Using of TImage.Canvas
« Reply #2 on: February 24, 2026, 09:49:00 pm »
TPaintBox seems do what I need. Thank you.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

LemonParty

  • Sr. Member
  • ****
  • Posts: 439
Re: Using of TImage.Canvas
« Reply #3 on: February 24, 2026, 09:58:08 pm »
I changed the class of TImage into TPaintBox and TPaintBox.Canvas.Rectangle do not output any visible changes. What I miss?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

LemonParty

  • Sr. Member
  • ****
  • Posts: 439
Re: Using of TImage.Canvas
« Reply #4 on: February 24, 2026, 10:15:23 pm »
I see that TPaintBox require to use OnPaint event. TImage seems easier to use. Is there a performance reasons to use TPaintBox over TImage?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Xenno

  • Jr. Member
  • **
  • Posts: 87
    • BS Programs
Re: Using of TImage.Canvas
« Reply #5 on: February 24, 2026, 11:16:59 pm »
I'm not sure about performance. Their purpose are just different. TImage for displaying image while TPaintBox for drawing.

When we draw in a TImage, we will mostlikely draw on Bitmap.Canvas inside Picture. The bitmap could has different dimension than TImage dimension.

It might be an advantage or disadvantage that TImage will provide stretching and centering but the Bitmap is not actually as shown. On the side, TPaintBox only has one dimension.
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

wp

  • Hero Member
  • *****
  • Posts: 13487
Re: Using of TImage.Canvas
« Reply #6 on: February 25, 2026, 12:26:50 am »
I would not use a TImage for painting - this control is way too complicated. As already proposed, use a TPaintbox and draw in its OnPaint event. If your drawing gets more and more features over time this may have the disadvantage that you will always have to redo the previously drawn parts which could have an impact on speed and cause some flicker. In this case, and when you wan to draw also immediately after a button click, you could use a buffer bitmap: Draw first into the bitmap, and in the Paintbox.OnPaint just draw the bitmap. Request an update of the paintbox by Paintbox.Invalidate.

Untested:
Code: Pascal  [Select][+][-]
  1. var
  2.   Buffer: TBitmap;
  3.  
  4. // in form's OnCreate
  5. Buffer := TBitmap.Create;
  6.  
  7. // in form's OnDestroy
  8. Buffer.Free;
  9.  
  10. // in button click: draw a random line with every click
  11. procedure TForm1.Button1Click(Sender: TObject);
  12. var
  13.   x1, x2, y1, y2: Integer;
  14. begin
  15.   x1 := random(FBuffer.Width);
  16.   x2 := random(FBuffer.Width);
  17.   y1 := random(FBuffer.Height);
  18.   y2 := random(FBuffer.Height);
  19.   Buffer.Canvas.Pen.Color := RGBToColor(Random($FF), Random($FF), Random($FF));
  20.   Buffer.Canvas.Line(x1, y1, x2, y2);
  21.   // Force the paintbox to redraw itself
  22.   Paintbox1.Invalidate;
  23. end;
  24.  
  25. // OnPaint event of the Paintbox: it just draws the buffer bitmap
  26. procedure TForm1.PaintboxPaint(Sender: TObject);
  27. begin
  28.   Canvas.Draw(0, 0, Buffer);
  29. end;
« Last Edit: February 25, 2026, 12:28:43 am by wp »

LemonParty

  • Sr. Member
  • ****
  • Posts: 439
Re: Using of TImage.Canvas
« Reply #7 on: February 25, 2026, 03:50:36 pm »
Understood.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018