0

This question was asked many years back, How to take a screenshot of the Active Window in Delphi?. I have copied the exact same code however I cannot get the screenshot to work. All I keep getting is a black color 32x20pixel, 3KB Jpeg file. My complete code is below, what am i doing wrong?

unit Unit1;

interface

 uses
   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
 TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
 { Public declarations }
         end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
const FullWindow = true;
var
Win : HWND;
DC: HDC;
Bmp: TBitmap;
 FileName: string;
 WinRect: TRect ;
 Width: Integer;
 Height: Integer;
begin


Form1.Hide;
try
  Application.ProcessMessages;
  Win := GetForegroundWindow;
  if FullWindow then
  begin
    Winapi.Windows.GetWindowRect(Win, WinRect);
DC := GetWindowDC(Win);
  end else
  begin
    Winapi.Windows.GetClientRect(Win, WinRect);
    DC := GetDC(Win);
  end;
  try

Width := WinRect.Right - WinRect.Left;
Height := WinRect.Bottom - WinRect.Top;
Bmp := TBitmap.Create;
try
  Bmp.Height := Height;
  Bmp.Width := Width;
  BitBlt(Bmp.Canvas.Handle, 0, 0, Width, Height, DC, 0, 0, SRCCOPY);
  FileName := 'Screenshot_'+
    FormatDateTime('mm-dd-yyy-hhnnss', Now());
  Bmp.SaveToFile(Format('C:\Screenshot\%s.bmp', [FileName]));
finally
  Bmp.Free;
end;
  finally
    ReleaseDC(Win, DC);
  end;
finally
  Form1.Show;
end;
end;

end.
  • The code for taking the screenshot is fine. You didn't include any error checking, probably just copied the code blindly without attempting to understand it or debug it. Why do so few programmers attempt to debug their code? Putting this code in a button handler is troublesome. Who knows what the foreground window is when you hide this form. Try instead running this code out of a timer. – David Heffernan Sep 07 '17 at 14:07
  • I have already stated that I have copied the code exactly from the other question asked. Though I just copied the exact code it does not mean I did not try to find the problem. I have tried editing the code and tried to find the problem for hours but no luck, which is why im here!!! – WhizLady201777 Sep 07 '17 at 14:12
  • So do some debugging. Identify the window returned by `GetForegroundWindow`. I bet it's not what you expect. – David Heffernan Sep 07 '17 at 14:13
  • Do you have any other applications open and showing on your desktop when you test? Or is your application the only one? – Jerry Dodge Sep 07 '17 at 15:24
  • 2
    Try @KenWhite's solution in this question: https://stackoverflow.com/questions/23410377/delphi-active-window-screenshot – karliwson Sep 07 '17 at 15:39
  • The code you used is poor. It does no error checking at all. The code I posted in the link @karliwson provided is much cleaner, and has been tested on Windows 7 and 10. Note that Windows 7 and above allow applications to block screen capture via `SetWindowDisplayAffinity` when passed the `WDA_MONITOR` flag, and if that is done the only way to capture the window is with an external camera (like the one in your smartphone). – Ken White Sep 07 '17 at 17:29

0 Answers0