首页 > 解决方案 > 在 Windows 锁定之前截取屏幕截图

问题描述

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

我试过了SystemEvents.SessionSwith,但当时窗口处理程序不再有效。

我还尝试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'

标签: c#windowsscreenshotlockscreen

解决方案


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


推荐阅读