Reputation: 1437
I'm just playing around a bit. I try to take a screenshot of the main monitor right before the user locks the machine. So far nothing worked.
I tried a SystemEvents.SessionSwith
but the window handler is not valid anymore at that time.
I also tried a LowLevelKeyboardProc
to catch Win+L, take the screenshot and then lock the machine myself - but Win+L seems to be a somehow protected shortcut.
This is what I've got so far
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");
}
Fails with System.ComponentModel.Win32Exception: 'The handle is invalid'
Upvotes: 1
Views: 135
Reputation: 1437
I ended up using my own shortcut (Win+Shift+L
) for taking the screenshot and then locking the machine. Thanks @Hans Passant for the hint.
Upvotes: 1