Change the image in the frame every time C #

I am creating a WinForm application that takes a photo of a person using a webcam, and I am trying to create a countdown effect. I have 4 images that I would like to skip, but it's pretty hard to do.

I use a timer for seconds, but everything that happens is this application is a little behind and then shows the last image. Does anyone know how I can do this?

Here is 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"); } 
+4
source share
5 answers

The Windows Timer class uses a message queue to notify that a timer has expired. And so you need the message loop to work in order to get the right number of timers. Therefore, you must set the counter variable as a class field, and then you can increase it inside the event handler. Something like that...

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

You have to use

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

I tested it and it worked, hope it helps you

+6
source

The problem is that you are spinning in a busy cycle while the timer is running. You should check the timer stop condition in the event handler.

I am also a little surprised that the code works. If you use System.Windows.Forms.Timer , you should not even get into the event handler, so the counter should not increase. Also, the counter value is not checked or updated properly. The while loop can be converted to an infinite loop.

+1
source

Found solution, no timer required. Thanks for 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; 
+1
source

SET "INTERVAL = 1000" in the timer properties means that your timer refreshes every 1000 ms. And then use if (second == 10) .....

0
source

All Articles