Forum > LCL
TForm.Canvas content into a Picture
Weitentaaal:
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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure MainForm.FormToImage();var Image: TBitmap;begin Image := TBitmap.Create; Image.SetSize(MainForm.Width, MainForm.Height); MainForm.PaintTo(Image.Canvas, 0, 0); Image.SaveToFile('screenshot.bmp');end;
You might have to adapt it a bit to account for the window border.
Weitentaaal:
--- Quote from: Moritz 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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure MainForm.FormToImage();var Image: TBitmap;begin Image := TBitmap.Create; Image.SetSize(MainForm.Width, MainForm.Height); MainForm.PaintTo(Image.Canvas, 0, 0); Image.SaveToFile('screenshot.bmp');end;
You might have to adapt it a bit to account for the window border.
--- End quote ---
thank you !
this did work but only when calling Form.Show first. Otherwise i got a Black Picture.
jamie:
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.
Weitentaaal:
--- Quote from: jamie 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.
--- End quote ---
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.
--- End quote ---
Navigation
[0] Message Index
[#] Next page