在 Windows 锁定之前截取屏幕截图

Take screenshot right before Windows locks

我只是玩玩而已。我尝试在用户锁定机器之前截取主监视器的屏幕截图。到目前为止没有任何效果。

我尝试了 SystemEvents.SessionSwith 但当时 window 处理程序不再有效。

我也试过 LowLevelKeyboardProc 捕捉 Win+L,截图然后自己锁定机器 - 但 Win+L 似乎是某种受保护的快捷方式。

这是我目前得到的

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
}

private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
    switch (e.Reason)
    {
        case SessionSwitchReason.SessionLock: TakeScreenShot(); break;
    }
}

private void TakeScreenShot()
{
    var screen = Screen.AllScreens.Single(x => x.Primary);
    var screenshot = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, PixelFormat.Format32bppArgb);
    var gfxScreenshot = Graphics.FromImage(screenshot);
    gfxScreenshot.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, screen.Bounds.Size, CopyPixelOperation.SourceCopy);
    screenshot.Save("tmp.png");
}

失败 System.ComponentModel.Win32Exception: 'The handle is invalid'

我最终使用自己的快捷方式 (Win+Shift+L) 截取屏幕截图,然后锁定机器。感谢@Hans Passant 的提示。