I am using the following code to capture a Form
screenshot:
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.Activate();
Application.DoEvents();
Bitmap screenShotBMP = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics screenShotGraphics = Graphics.FromImage(screenShotBMP);
screenShotGraphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
screenShotBMP.Save(imagePath, ImageFormat.Gif);
screenShotGraphics.Dispose();
screenShotBMP.Dispose();
this.TopMost = false;
this.FormBorderStyle = FormBorderStyle.Sizable;
this.WindowState = FormWindowState.Minimized;
When I am logged in to the server, it works fine (I can see the screenshots generated) But when its locked, the line:
screenShotGraphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
Gives the error "Handle is invalid".
I came to know that its because there is no desktop when no one is logged in. So can anyone let me know any alternatives?
What I have is a WinForms application, scheduled to run after every 5 mins and when it runs, I have to take a screenshot of the Form
.
Please let me know a soluion.
This is not going to be possible. When a workstation is locked, Winlogon switches to the secure desktop. In this case, like you said, there is no desktop of which to take a picture.
Not even administrators have access rights to the secure desktop, so you couldn't take a picture of it , even if you wanted to. The only possible way to work around the security restrictions you're running afoul of here is to rewrite the application as a service so that you would gain access rights to the secure desktop, but then you would run into another set of security restrictions: namely, that services aren't allowed to interact with the user in any way and therefore do not have a "desktop".
It's unclear what you even expect to happen in this scenario. If there's no desktop to take a picture of, what would you want it to take a picture of?
The bottom line is that Windows is a multi-user operating system and has been for decades now. You have to give up on this 16-bit conceptual model where there is only one "desktop" and everyone has full access privileges to everything.
There are other, better ways of monitoring what is happening on a workstation than taking periodic screenshots… Ways that actually work even. I can't make general suggestions because it depends on what exactly you're trying to monitor.
我刚刚测试了PrintWindow api,它在锁定桌面的情况下可以正常工作: C#PrintWindow返回黑色图像
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.