Jump to content
johnnydp

screen shot - reliable method

Recommended Posts

Hi,

 

Looking for some realiable method of making screenshot

BitBlt give sometimes blank screen of desktop 

Can anyone post working code of other method 

Speed is also important, I realize it might be little slower

Share this post


Link to post

@Larry Hengen This does not work with full desktop and form independent

@M.JoosThis could work, one disadvantage: Really modern only, for me wored from 8.1 (not 7)

 

Thanks for input guys.

Share this post


Link to post
8 hours ago, johnnydp said:

Hi,

 

Looking for some realiable method of making screenshot

BitBlt give sometimes blank screen of desktop 

Can anyone post working code of other method 

Speed is also important, I realize it might be little slower

One way that is fairly reliable is to fake a press of the print screen key. The main problem is that you have to get the image from the clipboard, and that of course overwrites any previous content.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Clipboard.Clear;
  PostKeyEx32(VK_SNAPSHOT, [], false);
  Application.ProcessMessages;  // 'ware timers!
  if Clipboard.HasFormat(CF_BITMAP) then
    image1.Picture.Bitmap.Assign(Clipboard)
  else
    label1.Caption := 'No image!';
end;

{!
<summary>
 PostKeyEx32 uses keybd_event to manufacture a series of key events
 matching the passed parameters.</summary>
<param name="key">
 is the virtual keycode of the key to send. For printable keys this is
 simply the ANSI code (Ord(character)). </param>
<param name="shift">
 encodes the state of the modifier keys. This is a set, so
 you can set several of these keys (shift, control, alt, mouse buttons)
 in tandem. The TShiftState type is declared in the Classes unit.</param>
<param name="specialkey">
 normally this should be False. Set it to True to pecify a key on the
 numeric keypad, for example.</param>
<remarks>
 The events go to the control with focus, even if it is part of another
 process.
 Note that for characters key is always the upper-case version of
 the character. Sending without any modifier keys will result in
 a lower-case character, sending it with [ssShift] will result
 in an upper-case character!  </remarks>
}
procedure PostKeyEx32(key: Word; const shift: TShiftState;
  specialkey: Boolean);
type
  TShiftKeyInfo = record
    shift: Byte;
    vkey: Byte;
  end;
  byteset = set of 0..7;
const
  shiftkeys: array[1..3] of TShiftKeyInfo =
  ((shift: Ord(ssCtrl); vkey: VK_CONTROL),
    (shift: Ord(ssShift); vkey: VK_SHIFT),
    (shift: Ord(ssAlt); vkey: VK_MENU));
var
  flag: DWORD;
  bShift: ByteSet absolute shift;
  i: Integer;
begin
  for i := 1 to 3 do begin
    if shiftkeys[i].shift in bShift then
      keybd_event(shiftkeys[i].vkey,
        MapVirtualKey(shiftkeys[i].vkey, 0),
        0, 0);
  end; { For }
  if specialkey then
    flag := KEYEVENTF_EXTENDEDKEY
  else
    flag := 0;

  keybd_event(key, MapvirtualKey(key, 0), flag, 0);
  flag := flag or KEYEVENTF_KEYUP;
  keybd_event(key, MapvirtualKey(key, 0), flag, 0);

  for i := 3 downto 1 do begin
    if shiftkeys[i].shift in bShift then
      keybd_event(shiftkeys[i].vkey,
        MapVirtualKey(shiftkeys[i].vkey, 0),
        KEYEVENTF_KEYUP, 0);
  end; { For }
end; { PostKeyEx32 }

 

Share this post


Link to post
4 hours ago, M.Joos said:

In modern Windows (I think they started with Win7) you could use the Windows Desktop Duplication API. A delphi wrapper can be found here https://github.com/tothpaul/Delphi/tree/master/DesktopDuplicationAPI

The Desktop Duplication API was actually introduced in Windows 8.  In Windows Vista and Windows 7, you can use the Magnification API instead (but with some restrictions).

 

Windows 10 build 1803 introduces a new Screen Capture API.

  • Thanks 1

Share this post


Link to post

Thanks for all,

@PeterBelow this method is preety cool but with big problem it will overwrite current potenrial image in clipboard...

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×