1 分钟后截图

Taking Screenshot after 1 minute

我有两个问题:

1.当按下一个键时,如何每 1 分钟截取一次屏幕截图,例如

  1. 10:00: -> key pressed -> Img1
  2. 10:01: -> key pressed -> Img2
  3. 10:02: -> key pressed -> Img3

2。假设我的程序运行 5-10 分钟

,我该如何迭代图像链
  string ImgPath = @"D:\"Img" + iteration + ".bmp";
  Bitmap btmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
  Graphics g = Graphics.FromImage(btmp);
  g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, btmp.Size, CopyPixelOperation.SourceCopy);

if (any key is pressed)
if (time difference is 1 min)
btmp.Save(ImgPath, System.Drawing.Imaging.ImageFormat.Bmp);

另外如果有更好的截屏方法欢迎分享

谢谢!

您要做的是在拍照时启动(或重新启动)Stopwatch。然后,无论何时按下某个键,您都会检查秒表是否已 运行 至少一分钟。如果有,您拍照并重置秒表。总体思路:

// Start the clock when the program starts.
private Stopwatch _pictureTimer = Stopwatch.StartNew();

// Wait this long between pictures
private readonly TimeSpan _pictureWaitTime = TimeSpan.FromMinutes(1.0);

// Come here when key is pressed.
if (_pictureTimer.Elapsed > _pictureWaitTime)
{
    // take the screen shot
    // and then reset the stopwatch
    _pictureTimer.Restart();
}

如果你想给图片编号,保留一个你每次都更新的变量。当程序启动时,你初始化它:

private int _pictureNumber = 1;

每当你拍照时,你都会增加它。也就是说,重置秒表后,只需执行:

_pictureNumber = pictureNumber + 1;