Recent

Author Topic: Get a screenshot of a form  (Read 7435 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Get a screenshot of a form
« on: March 30, 2015, 03:42:16 am »
Hi all

I want know how i could capture the form image and send it to the printer, if it is posible.

More explained:

I have my own form, with some controls( scrollable); i want capture the image how it looks in the screen and the send it to the printer stretched.

Any help is welcome.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: Get a screenshot of a form
« Reply #1 on: March 30, 2015, 05:43:19 am »
If you want to screenshot a form by pascal code - I don't know how to do that.
But you can easily do a screenshot by scrot (under Linux) or print+screen and pasting it into image editor (Paint or, better, Gimp under Windows).
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Get a screenshot of a form
« Reply #2 on: March 30, 2015, 05:51:21 am »
AFAIK TCustomForm has a GetFormImage method, which can be captured by a TBitmap.

From Embarcadero:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  FormImage: TBitmap;
begin
  FormImage := GetFormImage;
  try
    Clipboard.Assign(FormImage);
    Image1.Picture.Assign(Clipboard);
  finally
    FormImage.Free;
  end;
end;
« Last Edit: March 30, 2015, 05:56:02 am by typo »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Get a screenshot of a form
« Reply #3 on: March 30, 2015, 07:38:30 am »
AFAIK TCustomForm has a GetFormImage method, which can be captured by a TBitmap.

From Embarcadero:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  FormImage: TBitmap;
begin
  FormImage := GetFormImage;
  try
    Clipboard.Assign(FormImage);
    Image1.Picture.Assign(Clipboard);
  finally
    FormImage.Free;
  end;
end;
Hmm...it's all black here on qt, gtk2 and win32 widgetset.

Timewarp

  • Full Member
  • ***
  • Posts: 144
Re: Get a screenshot of a form
« Reply #4 on: March 30, 2015, 07:50:04 am »
Hmm...it's all black here on qt, gtk2 and win32 widgetset.
Why it doesn't work is still unknown

http://bugs.freepascal.org/view.php?id=25448

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Get a screenshot of a form
« Reply #5 on: March 30, 2015, 09:41:16 am »
This works in win32:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  lRect: TRect;
  lFormBitmap: TBitmap;
begin
  lRect := Bounds(0, 0, ClientWidth, ClientHeight);
  lFormBitmap := TBitmap.Create;
  try
    lFormBitmap.Width := ClientWidth;
    lFormBitmap.Height := ClientHeight;
    lFormBitmap.Canvas.CopyRect(lRect, Canvas, lRect);
    lFormBitmap.SaveToFile('D:\Programas\output.bmp');
  finally
    lFormBitmap.Free;
  end;
end;

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Get a screenshot of a form
« Reply #6 on: March 30, 2015, 02:38:14 pm »
This works too:

Code: [Select]
  Image1.Picture.Assign(GetFormImage);

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Get a screenshot of a form
« Reply #7 on: March 30, 2015, 03:03:58 pm »
I use GTK here and works fine with this code:
Code: [Select]
 
  MyCapture:=TBitMap.Create(application);
  MyCapture.SetSize(ClientWidth,ClientHeight);
  MyCapture.canvas.FillRect(0,0,ClientWidth,ClientHeight);
  self.PaintTo(MyCapture.canvas,0,0);
  MyCapture.SaveToFile('/home/bylaardt/MyFormImage.bmp');
  MyCapture.free;
felipemdc's code woks too.
« Last Edit: March 30, 2015, 03:54:46 pm by bylaardt »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Get a screenshot of a form
« Reply #8 on: March 30, 2015, 04:11:32 pm »
I use GTK here and works fine with this code:
Code: [Select]
 
  MyCapture:=TBitMap.Create(application);
  MyCapture.SetSize(ClientWidth,ClientHeight);
  MyCapture.canvas.FillRect(0,0,ClientWidth,ClientHeight);
  self.PaintTo(MyCapture.canvas,0,0);
  MyCapture.SaveToFile('/home/bylaardt/MyFormImage.bmp');
  MyCapture.free;
felipemdc's code woks too.
Your code works on qt, felipe's code doesn't (again, black image). Seems like PaintTo is the angel here, while CopyRect somewhat doesn't work everywhere. It's weird, though. I remember in the past using CopyRect too to get a partial "screenshot" of my forms. I'll have to dig in my old code.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Get a screenshot of a form
« Reply #9 on: March 30, 2015, 04:25:38 pm »
This works in win32:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  lRect: TRect;
  lFormBitmap: TBitmap;
begin
  lRect := Bounds(0, 0, ClientWidth, ClientHeight);
  lFormBitmap := TBitmap.Create;
  try
    lFormBitmap.Width := ClientWidth;
    lFormBitmap.Height := ClientHeight;
    lFormBitmap.Canvas.CopyRect(lRect, Canvas, lRect);
    lFormBitmap.SaveToFile('D:\Programas\output.bmp');
  finally
    lFormBitmap.Free;
  end;
end;

This one worked perfet for me. Now im trying to send this image to the printer. I guess printer4lazarus is the way but i can not figure how i could do that for myself.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Get a screenshot of a form
« Reply #10 on: March 30, 2015, 04:34:15 pm »
AFAIK TCustomForm has a GetFormImage method, which can be captured by a TBitmap.
Hmm...it's all black here on qt, gtk2 and win32 widgetset.
Just FYI:
For Win32 GetFormImage does work but assigning it first to the Clipboard and then Clipboard to your Image1 does give a black result. Assigning FormImage directly to Image1.Picture does works ok (in win32, not sure on qt and gtk2).

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  FormImage: TBitmap;
begin
  FormImage := GetFormImage;
  try
    // Clipboard.Assign(FormImage); // <=== NOPE
    Image1.Picture.Assign(FormImage); // <-- this works ok
  finally
    FormImage.Free;
  end;
end;

 

TinyPortal © 2005-2018