Recent

Author Topic: [Solved]with bitmap.canvas issue  (Read 607 times)

PeterHu

  • Jr. Member
  • **
  • Posts: 62
[Solved]with bitmap.canvas issue
« on: August 11, 2025, 08:15:20 am »
When I play around with lazarus/examples/canvas_test/canvastest.lpi project,I noticed the rectangletest formPaint event produce blow screenshot withCanvas1.png

But when I replaced all those
Code: Pascal  [Select][+][-]
  1. MyBitmap.Canvas.methodxxx
  2.  
with
Code: Pascal  [Select][+][-]
  1. with MyBitmap.Canvas do begin
  2. methodxxx;
  3. end;
  4.  


for example:
Code: Pascal  [Select][+][-]
  1. procedure TfrmRectangles.FormPaint(Sender: TObject);
  2. var
  3.   MyBitmap: TBitmap;
  4. begin
  5.   MyBitmap := TBitmap.Create;
  6.   try
  7.  
  8.     MyBitmap.Height := Height;
  9.     MyBitmap.Width := Width;
  10.  
  11.     with MyBitmap.Canvas do begin
  12.         Brush.Color := clWhite;
  13.         Pen.Color := clWhite;
  14.         Rectangle(0, 0, Width, Height);
  15.  
  16.         TextOut(100,  5, 'Brush: Red  Pen: Black  Grid: Blue');
  17.         TextOut( 10, 25, 'DrawFocusRect');
  18.         TextOut(125, 25, 'FillRect');
  19.         TextOut(225, 25, 'FrameRect');
  20.         TextOut(325, 25, 'Rectangle');
  21.  
  22.         Pen.Color := clBlue;
  23.  
  24.         Line( 0,  75, 400,  75);
  25.         Line( 0, 125, 400, 125);
  26.  
  27.         Line( 25, 40,  25, 150);
  28.         Line( 75, 40,  75, 150);
  29.         Line(125, 40, 125, 150);
  30.         Line(175, 40, 175, 150);
  31.         Line(225, 40, 225, 150);
  32.         Line(275, 40, 275, 150);
  33.         Line(325, 40, 325, 150);
  34.         Line(375, 40, 375, 150);
  35.  
  36.         Brush.Color := clRed;
  37.         Pen.Color := clBlack;
  38.         DrawFocusRect(Bounds(25, 75, 50, 50));
  39.         FillRect(Bounds(125, 75, 50, 50));
  40.         FrameRect(Bounds(225, 75, 50, 50));
  41.         Rectangle(Bounds(325, 75, 50, 50));
  42.      end;
  43.  
  44.     Canvas.Draw(0, 0, MyBitmap);
  45.   finally
  46.     MyBitmap.Free;
  47.   end;
  48. end;                    
  49.  
  50.  
  51.  
       

this will produe a very different look and feel as shown in screenshot withCavas2.png.

Attached my small test program for the reference.

Is there anything I made a mistake?

Help would be appreciated.
« Last Edit: August 11, 2025, 10:41:00 am by PeterHu »

bytebites

  • Hero Member
  • *****
  • Posts: 781
Re: with bitmap.canvas issue
« Reply #1 on: August 11, 2025, 08:40:58 am »
Change
   Rectangle(0, 0, Width, Height);
to
   Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);


PeterHu

  • Jr. Member
  • **
  • Posts: 62
Re: with bitmap.canvas issue
« Reply #2 on: August 11, 2025, 10:24:12 am »
Change
   Rectangle(0, 0, Width, Height);
to
   Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);

Fixed!Thank you.

Just learnt from mistakes and know that below width & height are just mybitmap.canvas.width ,mybitmap.canvas.height which are just 0.
From the doc:
Code: Pascal  [Select][+][-]
  1. TCanvas.Height
  2. Height of the canvas.
  3.  
  4. Declaration
  5. Source position: graphics.pp line 1205
  6. published
  7. property
  8. TCanvas.Height : Integer
  9.  
  10. read
  11. GetHeight;
  12.  
  13. Description
  14.  
  15. Height is a read-only Integer property which contains the vertical dimension in Pixels for the canvas drawing area. Height and Width determine the limits for the 2-dimensional coordinate system used to access the Pixels on the drawing surface.
  16.  
  17. The value for the property is determined by calling the GetDeviceSize routine using the Handle for the class instance.The property value is 0 (zero) when Handle has not been  assigned (contains 0).
  18.  
Code: Pascal  [Select][+][-]
  1. Rectangle(0,0,width,height);
  2.  
« Last Edit: August 11, 2025, 10:26:14 am by PeterHu »

cdbc

  • Hero Member
  • *****
  • Posts: 2787
    • http://www.cdbc.dk
Re: with bitmap.canvas issue
« Reply #3 on: August 11, 2025, 10:32:46 am »
Hi
That, or you'll just be using the Form's width & height, they're also within grasp of the 'with'-scope...!
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

PeterHu

  • Jr. Member
  • **
  • Posts: 62
Re: with bitmap.canvas issue
« Reply #4 on: August 11, 2025, 10:40:41 am »
Hi
That, or you'll just be using the Form's width & height, they're also within grasp of the 'with'-scope...!
Regards Benny

Noted with thanks.

 

TinyPortal © 2005-2018