Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screen capture with C# and Remote Desktop problems

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.

like image 1000
Kay Avatar asked Apr 11 '11 10:04

Kay


People also ask

How do I take a screenshot in C?

Take a screenshot using your keyboardPress the Windows key + PrintScreen on your keyboard (or, PrtSc). Screenshots are automatically saved to Pictures/Screenshots in your user directory (C:/Users/%username%/Pictures/Screenshots by default).

How do you take a screenshot on a laptop C?

Press Ctrl + PrtScn keys. The entire screen changes to gray including the open menu. Select Mode, or in earlier versions of Windows, select the arrow next to the New button. Select the kind of snip you want, and then select the area of the screen capture that you want to capture.

What is the keyboard shortcut for screen capture?

Take the Screenshot by using the Windows + Shift + S keyboard shortcut. If you're using Windows 10, press Windows + Shift + S at the same time to capture a portion of your screen and copy it to your clipboard.

How do I capture a screenshot in Windows 10?

The Windows key + Print Screen To take a screenshot on Windows 10 and automatically save the file, press the Windows key + PrtScn. Your screen will go dim and a screenshot of your entire screen will save to the Screenshots folder.


1 Answers

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:

  1. Disable RDC and configure windows to Autologin with an specified account
  2. Connect to this machine using another remote desktop application (e.g. TightVNC)

Does this help?

like image 196
Haplo Avatar answered Sep 20 '22 22:09

Haplo