Recent

Author Topic: Save ClipBoard Image to File  (Read 12123 times)

Pascal95

  • Guest
Save ClipBoard Image to File
« on: March 23, 2010, 03:30:32 pm »
Hello,

I'm going to make a function to save the entire Screen to a file.

First I simulate the keypress of the Printscreen Key:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  keybd_event(VK_SNAPSHOT , 0, 0, 0);
  keybd_event(VK_SNAPSHOT , 0, KEYEVENTF_KEYUP, 0);
end;


Now, I want to load the Clipboard into an Image like this:

Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
begin
  Image1.Picture.Assign(ClipBoard);
end;


However the following error occurs: http://img121.imageshack.us/img121/1074/errorwhenloadingscreens.png

I also tried to do it like this:

Code: [Select]
if Clipboard.HasFormat(CF_PICTURE) then ShowMessage('It is an image');And it showed me that an image is in the Clipboard. And, yes, I added the Clipboard to the uses.

I wonder why such easy thing won't work...

Would be kind if you can help me. Thanks

Pascal95 (using Lazarus IDE v0.9.28.2)

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1930

Pascal95

  • Guest
How to Save the Image in the ClipBoard to a File?
« Reply #2 on: March 23, 2010, 05:21:23 pm »
Thanks, but the question wasnt how to take a screenshot. And.. I got an error using your solution:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  MyBitmap: TBitmap;
  ScreenDC: HDC;
begin
  MyBitmap := TBitmap.Create(self);
  ScreenDC := GetDC(0);
  MyBitmap.LoadFromDevice(ScreenDC);
  ReleaseDC(ScreenDC);
end; 
 

It says:

Code: [Select]
unit1.pas(39,23) Error: Unknown record field identifier "CREATE"
unit1.pas(41,12) Error: Unknown record field identifier "LOADFROMDEVICE"
unit1.pas(42,21) Error: Wrong number of parameters specified for call to "ReleaseDC"
unit1.pas(58) Fatal: There were 3 errors compiling module, stopping

But how do I SAVE the image in the Clipboard to my computer?
Whats wrong with
Code: [Select]
Image1.Picture.Assign(ClipBoard);??

Thanks.

Pascal

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2614
Re: Save ClipBoard Image to File
« Reply #3 on: March 23, 2010, 10:56:26 pm »
About the errors: swap the Graphics and the Windows unit in your uses clause, so the Windows unit is first.
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Pascal95

  • Guest
Re: Save ClipBoard Image to File
« Reply #4 on: March 23, 2010, 11:17:48 pm »
Thx, Now I solved the problem like this:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var ScreenDC:HDC;
begin
ScreenDC:= GetDC(0);
image1.picture.bitmap.LoadFromDevice(ScreenDC);
ReleaseDc(0, ScreenDC);
Image1.Picture.SaveToFile('C:\temp\screen.bmp');
end;   

 

TinyPortal © 2005-2018