gpt4 book ai didi

C#:从 Windows 服务捕获屏幕

转载 作者:太空狗 更新时间:2023-10-30 00:36:28 25 4
gpt4 key购买 nike

我必须每隔一秒捕获一次桌面屏幕截图。在 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#:从 Windows 服务捕获屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1832384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com