问题
I have a C sharp console application that captures a screenshot of a MS Word document several times. It works great, but when I place this application on a remote windows XP machine it works fine whilst I am remoted in i.e. my remote desktop is visible but if I run my app and leave remote desktop (minimize it, not even log off which I want to do) the screenshots it takes are blank!
The Screenshot app is being run by a service that runs as SYSTEM user.
How can I keep the GUI alive for windows even when there are no users connected?
Here is the code I use:
public Image CaptureWindow(IntPtr handle)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest, hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
Update
I am currently making use of PrintWindow which is the only thing that has come the closest as it manages to capture the window frame (i.e the minimise, maximise and close buttons) but the inner part is black.
Although it hasn't fully worked, its proved to me that it is possible to create an image from a window handle whilst the application isn't even visible to a user.
回答1:
Some time ago we were doing something similar, and we found that when RDC is minimized, the remote desktop session is not redrawn or accept keys or mouse events. Everything was working fine until we minimized the RDC screen. A colleague found out that this is done for performance reasons.
Some days ago I stumbled upon this, but I haven't had the chance to try it. If you try and it works, please let me know :)
Interacting with remote desktop when RDC is minimized
Regarding your comments: I think this is another kind of issue... I understand that you need your application to work even if no one is logged into the machine. I've implemented services that are allowed to interact with the desktop, for example, to launch an application and automate it. Even no one is logged in the machine, you can still manipulate the UI, for example, with an UI automation library (or your code, I assume).
After starting the machine, when my service and automated application are running everything works fine. Later on, the UI being automated will appear on the desktop of the first person who logs in (I was a machine administrator, I don't know what will happen when somebody with less privileges logs in).
I don't know what will happen if the first login is done through RDC. Maybe you could try changing those RDC settings id this affects the behavior of your application. Another option is:
- Disable RDC and configure windows to Autologin with an specified account
- Connect to this machine using another remote desktop application (e.g. TightVNC)
Does this help?
来源:https://stackoverflow.com/questions/5620113/screen-capture-with-c-sharp-and-remote-desktop-problems