我正在做一个windows服务程序,它是每秒自动捕获屏幕并将其设置为目录。当我运行它只有一次。 我该怎么做?这是我的代码。C#创建客户端作为Windows服务
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' – ASh
对不起我的英文很烂!你想这样说#保护覆盖void OnStart(string [] args) { timer1.Start(); timer1_Tick(); }# –
是的,没错。我们仍然需要在秒通过之前的初始呼叫 – ASh