I am screen capturing as follows:
Code
Bitmap b = new Bitmap(ScreenWidth, ScreenHeight); g = Graphics.FromImage(b); g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size); b.Save(@"S:\\shot" + i.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); i = i + 1;The problem is when I lock the computer, it gives The handle is invalid exception for the line below:
Code
g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size);Best Regards.

VulpesRank #55 · 29.2K reputationPosted Jan 6, 2012, 4:12 AM EST
A cruder way would be to simply place the code in a 'try' block and catch (and swallow) the exception if the desktop is locked.
RaysefoRank #4,173 · 360 reputationPosted Jan 6, 2012, 4:00 AM EST
Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch); void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e) { if (e.Reason == SessionSwitchReason.SessionLock) { //I left my desk } else if (e.Reason == SessionSwitchReason.SessionUnlock) { //I returned to my desk } }RaysefoRank #4,173 · 360 reputationPosted Jan 6, 2012, 4:47 AM EST