[英]C#: capture screen from Windows service
我必須在每一秒后捕獲Desktop的屏幕截圖。 在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");
}
}
您是否選中了“允許服務與桌面交互”(在服務屬性中)?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.