Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

c# - windows service screen capture returns black screen

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...