【问题标题】:C#: capture screen from Windows serviceC#:从 Windows 服务捕获屏幕
【发布时间】:2009-12-02 11:35:32
【问题描述】:

我必须每隔一秒捕获一次桌面的屏幕截图。在 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");
        }
}

【问题讨论】:

标签: c# image-capture


【解决方案1】:

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

【讨论】:

  • 在 windows xp 上我知道这是解决方案,但我不确定它是否适用于 windows visa/7
  • 谢谢。此选项未选中。我如何使用代码检查它。
  • Petoj 是正确的。这适用于 XP,但 Vista/7 不允许这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 2017-03-15
相关资源
最近更新 更多