4

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 task manager

Community
  • 1
  • 1
DevT
  • 4,843
  • 16
  • 59
  • 92

1 Answers1

6

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.

Eight-Bit Guru
  • 9,756
  • 2
  • 48
  • 62
  • That is not the service - it is a debugging version of the code running in the vshost appdomain. I've never tried to run a service from the vshost environment, and I have no idea what Window Station it might be connected to in that scenario, but I'm sticking with my answer and betting that it's not the same as the active desktop you're looking at and expecting a screenshot of. – Eight-Bit Guru Sep 23 '13 at 14:53
  • yes u r correct... is there any way for me to change session ID? – DevT Sep 24 '13 at 08:01
  • You can't change sessions (I was uncharacteristically vague in my earlier answer) but you can have your service connect to a different Window Station and Desktop. Your second linked answer describes exactly how to do it. – Eight-Bit Guru Sep 24 '13 at 12:27
  • Finally i make this application as windows form application. we cant change process ID as u mentioned. thanks for info – DevT Oct 05 '13 at 05:29