【发布时间】:2011-04-11 10:39:27
【问题描述】:
我有一个 C sharp 控制台应用程序,它可以多次捕获 MS Word 文档的屏幕截图。它工作得很好,但是当我将此应用程序放在远程 Windows XP 机器上时,它工作正常,而我处于远程状态,即我的远程桌面是可见的,但是如果我运行我的应用程序并离开远程桌面(最小化它,甚至没有 log我想做的事)截图是空白的!
屏幕截图应用由以 SYSTEM 用户身份运行的服务运行。
即使没有用户连接,我如何才能让 Windows 的 GUI 处于活动状态?
这是我使用的代码:
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;
}
更新
我目前正在使用 PrintWindow,它是唯一一个最接近的东西,因为它设法捕获窗口框架(即最小化、最大化和关闭按钮),但内部是黑色的。
虽然它还没有完全发挥作用,但它向我证明了可以从窗口句柄创建图像,而用户甚至看不到应用程序。
【问题讨论】:
标签: c# .net windows remote-desktop system.drawing