ImageEn, unit imageenio

TImageEnIO.CaptureFromScreen

TImageEnIO.CaptureFromScreen


Declaration

function CaptureFromScreen(Source: TIECSSource = iecsScreen; MouseCursor: TCursor = -1; Window: THandle = 0): Boolean;


Description

Capture the desktop, specified screen or application window.
MouseCursor defines the cursor to draw because ImageEn cannot get it from Windows. Use -1 for none.
If Source is iecsSpecifiedWindow or iecsSpecifiedWindowClient, then Window specifies the handle of the window to capture.
Result is False if the capture fails.

Note: This method may give unpredictable results if your project has DPI Awareness set to "Unaware"


Demos

Demo  Demos\InputOutput\CaptureFromScreen\CaptureFromScreen.dpr
Demo  Demos\VideoCapture\DesktopToAvi\DesktopToAvi.dpr


Examples

// Save the current desktop to 'screen.png'
ImageEnView1.IO.CaptureFromScreen( iecsScreen, -1 );
ImageEnView1.IO.SaveToFile( 'D:\screen.png' );

// Capture the content of the window with the title, "Untitled - Notepad"
NotepadHandle := FindWindow( nil, 'Untitled - Notepad' );
if NotepadHandle <> 0 then
  ImageEnView1.IO.CaptureFromScreen( iecsSpecifiedWindow, crDefault, NotepadHandle );

// Capture the content of the active Windows settings window (iecsSpecifiedWindow2 works better with some windows types)
handle := FindWindow( nil, 'Settings' );
SetForegroundWindow( handle ); // Ensure no other window overlaps desired one
if NotepadHandle <> 0 then
  ImageEnView1.IO.CaptureFromScreen( iecsSpecifiedWindow2, crDefault, handle );

// Using a TTimer, capture the Window that is under the cursor
// It tries iecsSpecifiedWindow and falls back to iecsSpecifiedWindow2 if it fails
procedure TForm1.Timer1Timer(Sender: TObject);
var
  pt: TPoint;
  h: HWND;
begin
  GetCursorPos( pt );
  if PtInRect( Self.BoundsRect, pt ) then
    exit; // Over own window

  h := Windows.WindowFromPoint( pt ); // Handle of control under the mouse cursor
  if ImageEnView1.IO.CaptureFromScreen( iecsSpecifiedWindow, -1, h ) = False then
    ImageEnView1.IO.CaptureFromScreen( iecsSpecifiedWindow2, -1, h );
end;

// Capture each screen of a multi-monitor system and save to file
for i := 0 to Screen.MonitorCount - 1 do
begin
  ImageEnView1.IO.CaptureFromScreen( iecsSpecifiedMonitor, -1, i );
  ImageEnView1.IO.SaveToFile( format( 'D:\Screen_%d.jpeg', [ i ]));
end;

// Capture primary monitor to a TBitmap
var
  bmp: TBitmap;
  IO: TImageEnIO;
begin
  bmp := TBitmap.Create;
  IO := TImageEnIO.CreateFromBitmap( bmp );
  try
    IO.CaptureFromScreen( iecsPrimary );
    ... do something with bmp ...
  finally
    IO.Free;
    bmp.Free;
  end;
end;