简体   繁体   English

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

[英]Change image in picturebox every second C#

I'm creating a WinForm application that takes a person's photo with a webcam and am trying to now create a countdown effect. 我正在创建一个WinForm应用程序,它使用网络摄像头拍摄一个人的照片,我正在尝试创建一个倒计时效果。 I have 4 images that i would like to cycle through but this is proving quite tricky to accomplish. 我有4个图像,我想循环,但这是非常棘手的完成。

I'm using a timer for the seconds, but all that's happening is the app lags a bit and then the last image shows. 我正在使用计时器秒,但所有发生的事情是应用程序滞后一点,然后最后一张图像显示。 Does anybody know how i might accomplish this? 有谁知道我怎么做到这一点?

Here's my code: 这是我的代码:

        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();

    /// <summary>
    /// timer event to switch between the countdown images
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    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");
    }

You should use 你应该用

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

I tested it and it was working, hope it helps you 我测试了它并且它正在工作,希望它可以帮助你

The Windows Timer class uses the message queue for notifying the timer has expired. Windows Timer类使用消息队列通知计时器已过期。 And so you need to have the message loop running in order to get the correct number of timer expires occuring. 因此,您需要运行消息循环才能获得正确的计时器到期次数。 So you should set the counter variable to be a class field and then you can increment it inside the event handler. 因此,您应该将计数器变量设置为类字段,然后您可以在事件处理程序中增加它。 Something like this... 像这样......

    // 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");
    }

The problem is that you are spinning in a busy loop while the timer is running. 问题是在计时器运行时你正在一个繁忙的循环中旋转。 You should check the timer stop condition in the event handler. 您应该检查事件处理程序中的计时器停止条件。

I am also a bit surprised that the code works. 我也有点惊讶代码工作。 If you are using System.Windows.Forms.Timer , you should not even get into the event handler and so the counter should not be incremented. 如果您使用的是System.Windows.Forms.Timer ,则您甚至不应该进入事件处理程序,因此不应增加计数器。 Also the counter value is not properly checked nor updated. 此外,计数器值未正确检查或更新。 The while loop can be transformed into endless loop. while循环可以转换为无限循环。

Found a solution, no timer required. 找到了解决方案,无需计时器。 Thanks for the answers. 谢谢你的回答。

        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).....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM