Change image in picturebox every second C#

后端 未结 5 1527
一整个雨季
一整个雨季 2021-01-13 13:08

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 thr

相关标签:
5条回答
  • 2021-01-13 13:24

    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

    0 讨论(0)
  • 2021-01-13 13:24

    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.

    0 讨论(0)
  • 2021-01-13 13:30

    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;
    
    0 讨论(0)
  • 2021-01-13 13:36

    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");
        }
    
    0 讨论(0)
  • 2021-01-13 13:44

    SET "INTERVAL =1000" in timer properties that's mean your timer every 1000 ms is refreshing And Then use an if(second == 10).....

    0 讨论(0)
提交回复
热议问题