Windows服务屏幕捕获返回黑屏

我正在尝试创建Windows服务应用程序来捕获屏幕。 以前我有启动服务的问题 。 无论如何我能够解决它,现在我有另一个问题。 现在图像正在保存,但它保存为黑屏。 为此,在SOF中提出了很多问题,但我无法解决我的问题。

这是我到目前为止所尝试的:

public partial class ScreenCaptureService : ServiceBase { private static Bitmap bmpScreenshot; //private static Graphics gfxScreenshot; System.Timers.Timer timer = new System.Timers.Timer(); public ScreenCaptureService() { InitializeComponent(); } protected override void OnStart(string[] args) { TraceService(); timer.Elapsed += new ElapsedEventHandler(OnElapsedTime); timer.Interval = 60000; timer.Enabled = true; } protected override void OnStop() { timer.Enabled = false; TraceService(); } private void TraceService() { Desktop userDesk = new Desktop(); userDesk.BeginInteraction(); string path = @"D:\Screen\"; if (!Directory.Exists(path)) Directory.CreateDirectory(path); string fileName = string.Format("SCR-{0:yyyy-MM-dd_hh-mm-ss-tt}.png", DateTime.Now); string filePath = path + fileName; bmpScreenshot = CaptureScreen.GetDesktopImage(); bmpScreenshot.Save(filePath, ImageFormat.Png); userDesk.EndInteraction(); } private void OnElapsedTime(object source, ElapsedEventArgs e) { TraceService(); } } 

在这里,我遵循在这里和这里提到的代码。 但它对我不起作用。

我正在使用Windows 7个。 我看到几个关于the session 0 isolation feature答案,但我无法从他们那里得到适当的解决方案。

在这里编辑此服务作为session 0运行 任务管理器

服务在会话0中运行,该会话具有与其他会话不同的Window Station和Desktop分配,其中用户通过其可见桌面与系统交互。

您可能希望让您的服务切换到活动用户的会话以建立到其可见桌面的链接,以便创建它的快照 – 您的屏幕截图代码按原样运行,但正在拍摄快照自己的桌面(什么都不是)。

这可能有助于为您澄清一些事情。