【发布时间】:2016-02-05 07:13:41
【问题描述】:
我正在做一个 windowsService 程序,它每隔 A 秒自动截屏并将其设置在 dir 上。当我运行它时,它只工作一次。 我怎样才能做到?这是我的代码。
protected override void OnStart(string[] args)
{
timer1_Tick();
}
private void timer1_Tick()
{
string myDir = "c:\\Newfolder\\photo";
System.IO.Directory.CreateDirectory(myDir);
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
string fileName = string.Format(@"c:\Newfolder\photo\Screenshot" +"_" + DateTime.Now.ToString("(dd_MMMM_hh_mm_ss_tt)") + ".png");
bitmap.Save(fileName, ImageFormat.Png);
}
【问题讨论】:
-
一开始你就打电话给
timer1_Tick一次。不要那样做 - 改为启动计时器:timer1.Start();它会打勾并每次调用timer1_Tick -
对不起我的英语有点糟糕!你想说这样 #protected override void OnStart(string[] args) { timer1.Start(); timer1_Tick(); }#
-
是的,没错。我们仍然需要在 A 秒过去之前进行初始调用
标签: c#