窗口服务屏幕捕获返回黑屏 [英] windows service screen capture returns black screen
问题描述
我试图创建Windows服务应用程序捕获屏幕。以前我曾启动该服务的问题。好歹我能解决它,现在我有另一个问题。现在,图像保存,但保存为黑屏。对于这个也有很多的SOF问的问题,但我没能解决我的问题。
I'm trying to create windows service application to capture screen. Previously i had problem of starting the service. Anyhow I'm able to solved it and now I'm having another problem. Now image is saving but it saves as a black screen. for this also there's lot of questions asked in SOF, but i couldn't able to solve my problem.
下面是我迄今为止尝试:
Here what i have tried so far:
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();
}
}
in here i followed codes mentioned in Here and Here. but it don't works for me.
我使用Windows 7 PC。我看到了有关提到的几种答案会话0隔离特性
但我无法从他们那里得到妥善解决。
I'm using windows 7 pc. i saw several answers mentioned about the session 0 isolation feature
but i couldn't get proper solution from them.
修改
这里这个服务作为会话0
EDIT
here this service runs as session 0
推荐答案
服务在会话0中,其中有一个不同的窗口站和桌面分配到用户通过他们的可见的桌面系统交互的其他会话中运行。
Services run in Session 0, which has a different Window Station and Desktop assignment to other sessions where users interact with the system through their visible desktops.
您'会可能希望让您的服务切换到一个活跃用户的会话建立一个链接到他们的可见的桌面,以创建它的快照 - 你的屏幕截图代码工作原样,但拍摄快照,它自己的桌面(其中什么)。
You'll probably want to have your service switch to an active users' session to establish a link to their visible desktop in order to create a snapshot of it - your screenshot code is working as-is, but is taking a snapshot its own Desktop (which is nothing).
This might help clarify things for you.
这篇关于窗口服务屏幕捕获返回黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!