2009-10-20 72 views
2

我在客户机上运行了Windows服务。我需要捕获客户端的屏幕并通过远程将其发送到服务器。当我运行exe文件时,它可以捕获屏幕并将其发送给服务器。但是,当我将其作为服务运行时,它会记录以下错误:无法捕获屏幕

“句柄无效。”

该服务的“与桌面交互”复选框被选中。我用于截图的代码是:

Image bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb); 
Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot); 
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); 
return bmpScreenshot; 

可能是什么原因以及如何解决此问题?

回答

5

服务运行在会话0(截至Vista),而其他应用程序不运行。

您需要在使用(例如)Run注册表项登录后,在用户会话中运行屏幕捕获应用程序。

+1

你现在正式成为我们公司当天的英雄。 – 2009-10-20 16:34:13

+0

我不确定这是如何回答这个问题的。决议是什么? – 2009-12-30 05:36:08

+0

@Michael我为这个问题增加了一个可能的解决方案。 – 2009-12-30 06:29:57