Solved

Screenshot or Alt and Printscreen

Posted on 2001-08-29
4
1,585 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanx for all the code examples!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now