简体   繁体   中英

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. I have 4 images that i would like to cycle through but this is proving quite tricky to accomplish.

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. 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. Also the counter value is not properly checked nor updated. The while loop can be transformed into endless loop.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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