windows service screen capture returns black screen

给你一囗甜甜゛ 提交于 2019-11-27 08:08:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!