如何使用C#使用计时器捕获屏幕?

ank*_*ush 1 c# winforms

这是一个使用C#的Windows应用程序.我想用计时器拍摄一个屏幕截图.定时器设置为5000 ms间隔.启动计时器时,应使用源窗口标题捕获桌面屏幕.

try
{
    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
    timer.Tick += new EventHandler(timer2_Tick);
    timer.Interval = (100) * (50);
    timer.Enabled = true;
    timer.Start();

    ScreenShots sc = new ScreenShots();
    sc.pictureBox1.Image = system_serveillance.CaptureScreen.GetDesktopImage();

    while(sc.pictureBox1.Image != null)
    {
        sc.pictureBox1.Image.Save("s"+".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        sc.pictureBox1.Image = null;
    }
Run Code Online (Sandbox Code Playgroud)

此代码无法正常运行.我怎样才能使它工作?

小智 6

计时器未触发,因为您没有处理tick事件.Pete还指出你的文件将在每个tick上被覆盖.

它需要看起来更像以下内容.这不是确切的代码,但它应该给你一个想法.

    private Int32 pictureCount = 0;

    public Form1()
    {
        timer1.Tick += new EventHandler(this.timer1_Tick);
        timer1.Interval = (100) * (50);
        timer1.Enabled = true;
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        /* Screen capture logic here */
        sc.pictureBox1.Image.Save(pictureCount.ToString() + ".jpg", ImageFormat.Jpeg);
        pictureCount++;
    }
Run Code Online (Sandbox Code Playgroud)