Recent

Author Topic: TForm.Canvas content into a Picture  (Read 626 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 539
TForm.Canvas content into a Picture
« on: September 03, 2024, 03:01:11 pm »
Hello,

I have a form which should display the dimensioning of an object with TLabel and TShape. Simple and 2D. Is it possible to get the content of the canvas as an image. Something like a screenshot of the form as an image.

Otherwise I will try it with fpvectorial.

Moritz

  • New member
  • *
  • Posts: 9
Re: TForm.Canvas content into a Picture
« Reply #1 on: September 03, 2024, 03:25:08 pm »
Hello,

you can paint the canvas of a form onto the canvas of a bitmap and then save it as a file with the following code:

Code: Pascal  [Select][+][-]
  1. procedure MainForm.FormToImage();
  2. var
  3.   Image: TBitmap;
  4. begin
  5.   Image := TBitmap.Create;
  6.   Image.SetSize(MainForm.Width, MainForm.Height);
  7.   MainForm.PaintTo(Image.Canvas, 0, 0);
  8.   Image.SaveToFile('screenshot.bmp');
  9. end;
  10.  

You might have to adapt it a bit to account for the window border.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 539
Re: TForm.Canvas content into a Picture
« Reply #2 on: September 03, 2024, 04:14:53 pm »
Hello,

you can paint the canvas of a form onto the canvas of a bitmap and then save it as a file with the following code:

Code: Pascal  [Select][+][-]
  1. procedure MainForm.FormToImage();
  2. var
  3.   Image: TBitmap;
  4. begin
  5.   Image := TBitmap.Create;
  6.   Image.SetSize(MainForm.Width, MainForm.Height);
  7.   MainForm.PaintTo(Image.Canvas, 0, 0);
  8.   Image.SaveToFile('screenshot.bmp');
  9. end;
  10.  

You might have to adapt it a bit to account for the window border.

thank you !

this did work but only when calling Form.Show first. Otherwise i got a Black Picture.

jamie

  • Hero Member
  • *****
  • Posts: 6587
Re: TForm.Canvas content into a Picture
« Reply #3 on: September 03, 2024, 05:44:15 pm »
That method only prints what is in view for the most part.

What you should be doing is using a internal bitmap that holds all of that, use the canvas of the bitmap as you would the screen.

The OnPaint event for the form, you simply paint that bitmap to the form.

when you print or save, you can simply use the internal bitmap.


 The other way around that is to create a procedure that receives a Canvas as a parameter and with that, you do the same as you would on your screen. The difference is, you can send the canvas of a bitmap, the form or a printer canvas and all should behave the same.


The only true wisdom is knowing you know nothing

Weitentaaal

  • Hero Member
  • *****
  • Posts: 539
Re: TForm.Canvas content into a Picture
« Reply #4 on: September 04, 2024, 07:13:29 am »
That method only prints what is in view for the most part.

What you should be doing is using a internal bitmap that holds all of that, use the canvas of the bitmap as you would the screen.

The OnPaint event for the form, you simply paint that bitmap to the form.

when you print or save, you can simply use the internal bitmap.


 The other way around that is to create a procedure that receives a Canvas as a parameter and with that, you do the same as you would on your screen. The difference is, you can send the canvas of a bitmap, the form or a printer canvas and all should behave the same.

i do not need the form. i have to draw a structure and i found it hard to do so with fpVectorial (i use it for Document creation, which does need that Drawing now). So i thought about creating a form since the drawing will be the same every time, but some information (TLabels which describe Dimensions of the Drawing) will change. So i would have to Set the Label Captions and then print the content of the form as Picture and use it in my TvVectorialDocument as Picture. So i think if i understand correctly what u suggest then i would draw everything inside the Bitmap ? or what exactly do u mean by
Quote
What you should be doing is using a internal bitmap that holds all of that, use the canvas of the bitmap as you would the screen.

Thaddy

  • Hero Member
  • *****
  • Posts: 15735
  • Censorship about opinions does not belong here.
Re: TForm.Canvas content into a Picture
« Reply #5 on: September 04, 2024, 07:54:58 am »
You need one of two things: either the bitmap from the canvas picture - which contrary to the above also stores the part that is not in sight, it stores the whole bitmap, or the svg representation. The latter only makes sense if the other software supports svg. svg is not a pure bitmap but a mathematical representation of an image. ( vector drawing format).
Bitmaps can be saved directly from the canvas, bitmap.savetofile.
« Last Edit: September 04, 2024, 07:58:40 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018