Solved

Screenshot or Alt and Printscreen

Posted on 2001-08-29
4
1,598 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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