C#:从Windows服务捕获屏幕

C#:从Windows服务捕获屏幕,c#,image-capture,C#,Image Capture,我必须在每一秒钟后捕获桌面的截图。在Winform应用程序中,它运行良好。但在将代码移动到Windows服务后,它并没有捕获屏幕截图。你知道为什么不这么做吗 这是密码 public partial class ScreenCaptureService : ServiceBase { System.Timers.Timer timer = new System.Timers.Timer(); public ScreenCaptureService()

我必须在每一秒钟后捕获桌面的截图。在Winform应用程序中,它运行良好。但在将代码移动到Windows服务后,它并没有捕获屏幕截图。你知道为什么不这么做吗

这是密码

public partial class ScreenCaptureService : ServiceBase
    {
        System.Timers.Timer timer = new System.Timers.Timer();

        public ScreenCaptureService()
        {
            InitializeComponent();            
            this.timer.Interval = 1000;
            this.timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            CaptureScreen();
        }

        protected override void OnStart(string[] args)
        {
            if (!EventLog.SourceExists(this.ServiceName, Environment.MachineName))
            {
                EventLog.CreateEventSource(
                    new EventSourceCreationData(
                        this.ServiceName,
                        Environment.MachineName
                        )
                );
            }

            EventLog.WriteEntry(this.ServiceName, "The OnStart event has been called");
            this.timer.Enabled = true;
            CaptureScreen();
        }

        protected override void OnStop()
        {
            EventLog.WriteEntry(this.ServiceName, "The OnStop event has been called");
            this.timer.Enabled = false;
        }

        static int count = 1;
        private void CaptureScreen()
        {

            Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

            Graphics graphics = Graphics.FromImage(printscreen as Image);

            graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);

            printscreen.Save(@"C:\printscreen" + count++ + ".jpg", ImageFormat.Jpeg);

            EventLog.WriteEntry(this.ServiceName, "Screenshot Captured");
        }
}

您是否选中了“允许服务与桌面交互”(在服务属性中)?

在windows xp上,我知道这是解决方案,但我不确定它是否适用于windows visa/7谢谢。此选项未选中。如何使用代码检查它。Petoj是正确的。这将在XP上运行,但Vista/7不允许这样做。另请参见