每秒更改图片框中的图像C#

我正在创建一个WinForm应用程序,用网络摄像头拍摄一个人的照片,我现在正试图创建一个倒计时效果。 我有4个图像,我想循环,但这是非常棘手的完成。

我正在使用计时器秒,但所有发生的事情是应用程序滞后一点,然后最后一张图像显示。 有谁知道我怎么做到这一点?

这是我的代码:

int counter = 0; // start the counter to swap the images tmCountDown.Start(); while (counter < 4) { // holding off picture taking } // reset counter for timer counter = 0; tmCountDown.Stop(); ///  /// timer event to switch between the countdown images ///  ///  ///  private void tmCountDown_Tick(object sender, EventArgs e) { counter++; //MessageBox.Show("c:/vrfid/apppics/" + counter + ".jpg"); pbCountDown.Image = new Bitmap("c:/vrfid/apppics/" + counter + ".jpg"); } 

Windows Timer类使用消息队列通知计时器已过期。 因此,您需要运行消息循环才能获得正确的计时器到期次数。 因此,您应该将计数器变量设置为类字段,然后您可以在事件处理程序内增加它。 这样的东西……

  // Main Code _counter = 0; tmCountDown.Start(); // Event Handler private void tmCountDown_Tick(object sender, EventArgs e) { _counter++; if (_counter == 4) tmCountDown.Stop(); else pbCountDown.Image = new Bitmap("c:/vrfid/apppics/" + _counter + ".jpg"); } 

你应该用

  counter++; this.SuspendLayout(); pbCountDown.Image = new Bitmap("c:/vrfid/apppics/" + counter + ".jpg"); this.ResumeLayout(); 

我测试了它,它正在工作,希望它可以帮助你

问题是在计时器运行时你正在一个繁忙的循环中旋转。 您应该检查事件处理程序中的计时器停止条件。

我也有点惊讶代码工作。 如果您使用的是System.Windows.Forms.Timer ,则您甚至不应该进入事件处理程序,因此不应增加计数器。 此外,计数器值未正确检查或更新。 while循环可以转换为无限循环。

找到了解决方案,无需计时器。 谢谢你的回答。

  int counter = 0; // start the counter to swap the images while (counter < 4) { // holding off picture taking counter++; //MessageBox.Show("c:/vrfid/apppics/" + counter + ".jpg"); pbCountDown.Image = new Bitmap("c:/vrfid/apppics/" + counter + ".jpg"); pbCountDown.Refresh(); Thread.Sleep(1000); } // reset counter for timer counter = 0; 

在计时器属性中设置“INTERVAL = 1000”,这意味着你的计时器每1000毫秒刷新然后使用if(second == 10)…..