C#:从Windows服务捕获屏幕
我必须每隔一秒捕获一次桌面的截图。 在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#学习教程:C#:从Windows服务捕获屏幕分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/983728.html