windows 带有 C# 和远程桌面问题的屏幕截图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5620113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:37:06  来源:igfitidea点击:

Screen capture with C# and Remote Desktop problems

c#.netwindowsremote-desktopsystem.drawing

提问by Kay

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!

我有一个 C 锐控制台应用程序,它多次捕获 MS Word 文档的屏幕截图。它工作得很好,但是当我将此应用程序放置在远程 Windows XP 机器上时,它可以正常工作,而我是远程桌面,即我的远程桌面是可见的,但是如果我运行我的应用程序并离开远程桌面(最小化它,甚至不注销我想做)它所需要的截图是空白的!

The Screenshot app is being run by a service that runs as SYSTEM user.

Screenshot 应用程序由以 SYSTEM 用户身份运行的服务运行。

How can I keep the GUI alive for windows even when there are no users connected?

即使没有用户连接,如何使 Windows 的 GUI 保持活动状态?

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.

我目前正在使用 PrintWindow 这是唯一一个最接近的东西,因为它设法捕获窗口框架(即最小化、最大化和关闭按钮),但内部部分是黑色的。

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.

虽然它还没有完全工作,但它向我证明了可以从窗口句柄创建图像,而应用程序甚至对用户不可见。

采纳答案by Haplo

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.

前段时间我们在做类似的事情,发现当RDC最小化时,远程桌面会话不重绘也不接受按键或鼠标事件。一切正常,直到我们最小化 RDC 屏幕。一位同事发现这样做是出于性能原因。

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

最小化 RDC 时与远程桌面交互

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).

关于您的评论:我认为这是另一种问题......我知道即使没有人登录机器,您也需要您的应用程序工作。我已经实现了允许与桌面交互的服务,例如,启动应用程序并使其自动化。即使没有人登录机器,您仍然可以操作 UI,例如,使用 UI 自动化库(或您的代码,我假设)。

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).

启动机器后,当我的服务和自动化应用程序运行时,一切正常。稍后,自动化的UI会出现在第一个登录的人的桌面上(我是机器管理员,不知道权限低的人登录会发生什么)。

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:

我不知道如果第一次登录是通过 RDC 完成的会发生什么。也许您可以尝试更改这些 RDC 设置 ID,这会影响您的应用程序的行为。另一种选择是:

  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)
  1. 禁用 RDC 并将 windows 配置为使用指定帐户自动登录
  2. 使用另一个远程桌面应用程序(例如 TightVNC)连接到这台机器

Does this help?

这有帮助吗?