?
Solved

Screenshot or Alt and Printscreen

Posted on 2001-08-29
4
Medium Priority
?
1,615 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 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

807 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