How can I take a screenshot in a screensaver application in Delphi?

119 Views Asked by At

My code shows a black image when Windows runs the screensaver after a wait. But it works when I click preview.

function CaptureScreenToStream: TMemoryStream;
var
  DesktopDC: HDC;
  CaptureBitmap: TBitmap;
  Width, Height: Integer;
begin
  DesktopDC := GetDC(GetDesktopWindow);
  try

    Width := round(GetDeviceCaps(DesktopDC, HORZRES) * strtoint(GetLastLoadedDPI) / 96);
    Height := round(GetDeviceCaps(DesktopDC, VERTRES) * strtoint(GetLastLoadedDPI) / 96);

    CaptureBitmap := TBitmap.Create;
    try
      CaptureBitmap.Width := Width;
      CaptureBitmap.Height := Height;
      BitBlt(CaptureBitmap.Canvas.Handle, 0, 0, Width, Height, DesktopDC, 0, 0, SRCCOPY);
      Result := TMemoryStream.Create;
      CaptureBitmap.SaveToStream(Result);
      Result.Position := 0;
    finally
      CaptureBitmap.Free;
    end;
  finally
    ReleaseDC(GetDesktopWindow, DesktopDC);
  end;
end;
0

There are 0 best solutions below