Solved

Screenshot or Alt and Printscreen

Posted on 2001-08-29
4
1,594 Views
Last Modified: 2010-05-18
I want to take a desktop screenshot or simulate alt + printscreen from Delphi 5. Are there any API calls or Delphi code to do this? Thanx, Magnus.
0
Comment
Question by:skynergy
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 50 total points
ID: 6435487
didn't try to compile it, but I think it will work:

uses
  Windows, clipbrd;

procedure CaptureScreen(WholeDesktop: Boolean);
var
  Desktop: HWND;
  DesktopDC: HDC;
  FWidth, FHeight, FLeft, FTop: Integer;
  AImage: TBitmap;
  WindowRect: TRect;
  AFormat: Word;
  AData, APalette : THandle;
begin
  Desktop := GetDesktopWindow;

  AImage := TBitmap.Create;
  if WholeDesktop then
  begin
    FWidth := Screen.Width;
    FHeight := Screen.Height;
    FLeft := 1;
    FTop := 1;
  end else
  begin
    GetWindowRect(GetTopWindow(0), WindowRect);
    FWidth := WindowRect.Right - WindowRect.Left;
    FHeight := WindowRect.Bottom - WindowRect.Top;
    FLeft := WindowRect.Left;
    FTop := WindowRect.Top;
  end;

  DesktopDC := GetWindowDC(Desktop);
  try
    AImage.Width := FWidth;
    AImage.Height := FHeight;
    BitBlt(AImage.Canvas.Handle, 1, 1, FWidth, FHeight,
      DesktopDC, FLeft, FTop, SRCCOPY);
    AImage.SaveToClipBoardFormat(AFormat, AData, APalette);
    ClipBoard.SetAsHandle(AFormat, AData);
  finally
    ReleaseDC(Desktop, DesktopDC);
    AImage.Free;
  end;
end;
0
 

Author Comment

by:skynergy
ID: 6435588
Hi there. When compiling I'm geting the following error: "[Error] Functions.pas(1189): Types of actual and formal var parameters must be identical" for "AImage.SaveToClipBoardFormat(AFormat, AData, APalette);".

I have changed the procedure slighty for my needs:

procedure CaptureScreen(csWholeDesktop: Boolean; var csBitMap: TImage);
var
 Desktop: HWND;
 DesktopDC: HDC;
 FWidth, FHeight, FLeft, FTop: Integer;
 AImage: TBitmap;
 WindowRect: TRect;
 AFormat: Word;
 AData, APalette : THandle;
begin
 Desktop := GetDesktopWindow;

 AImage := TBitmap.Create;
 if csWholeDesktop then
 begin
   FWidth := Screen.Width;
   FHeight := Screen.Height;
   FLeft := 0;
   FTop := 0;
 end else
 begin
   GetWindowRect(GetTopWindow(0), WindowRect);
   FWidth := WindowRect.Right - WindowRect.Left;
   FHeight := WindowRect.Bottom - WindowRect.Top;
   FLeft := WindowRect.Left;
   FTop := WindowRect.Top;
 end;

 DesktopDC := GetWindowDC(Desktop);
 try
   AImage.Width := FWidth;
   AImage.Height := FHeight;
   BitBlt(AImage.Canvas.Handle, 0, 0, FWidth, FHeight,
     DesktopDC, FLeft, FTop, SRCCOPY);
 finally
   ReleaseDC(Desktop, DesktopDC);
   csBitMap.Picture.Assign(AImage);
   AImage.Free;
 end;
end;

You wil also notice that I changed all the 1's to 0's because there was a grey line on the left for a whole desktop capture.

Thanx a lot for this code!
Magnus
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6439132
ah, so instead of capturing to clipboard, you actually wanted to save it into a file :)

I would advise putting
  csBitMap.Picture.Assign(AImage);
after the bitblt statement instead of in the finally statement

Oh, and my mistake, I should've put in 0, 0 instead of 1, 1 as the starting point :)



DragonSlayer.
0
 

Author Comment

by:skynergy
ID: 6522737
Thanx for all the code examples!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi: how to implement a User Shortcut mapper? 1 121
control image tags in a string ? 12 142
CheckListBox usage 3 72
RESTRequest Parameter 4 43
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question