问题
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.
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.
EDIT
here this service runs as session 0
回答1:
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.
来源:https://stackoverflow.com/questions/18891819/windows-service-screen-capture-returns-black-screen