Forum > General
Save ClipBoard Image to File
(1/1)
Pascal95:
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: ---procedure TForm1.Button1Click(Sender: TObject);
begin
keybd_event(VK_SNAPSHOT , 0, 0, 0);
keybd_event(VK_SNAPSHOT , 0, KEYEVENTF_KEYUP, 0);
end;
--- End code ---
Now, I want to load the Clipboard into an Image like this:
--- Code: ---procedure TForm1.Button2Click(Sender: TObject);
begin
Image1.Picture.Assign(ClipBoard);
end;
--- End code ---
However the following error occurs: http://img121.imageshack.us/img121/1074/errorwhenloadingscreens.png
I also tried to do it like this:
--- Code: ---if Clipboard.HasFormat(CF_PICTURE) then ShowMessage('It is an image');
--- End code ---
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:
http://wiki.lazarus.freepascal.org/Developing_with_Graphics#Taking_a_screenshot_of_the_screen
Pascal95:
Thanks, but the question wasnt how to take a screenshot. And.. I got an error using your solution:
--- Code: ---procedure TForm1.Button1Click(Sender: TObject);
var
MyBitmap: TBitmap;
ScreenDC: HDC;
begin
MyBitmap := TBitmap.Create(self);
ScreenDC := GetDC(0);
MyBitmap.LoadFromDevice(ScreenDC);
ReleaseDC(ScreenDC);
end;
--- End code ---
It says:
--- Code: ---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
--- End code ---
But how do I SAVE the image in the Clipboard to my computer?
Whats wrong with
--- Code: ---Image1.Picture.Assign(ClipBoard);
--- End code ---
??
Thanks.
Pascal
Marc:
About the errors: swap the Graphics and the Windows unit in your uses clause, so the Windows unit is first.
Pascal95:
Thx, Now I solved the problem like this:
--- Code: ---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;
--- End code ---
Navigation
[0] Message Index