Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change image in picturebox every second C#

Tags:

c#

winforms

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");
    }
like image 292
MattBH Avatar asked Dec 21 '22 12:12

MattBH


1 Answers

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

like image 101
Vamsi Avatar answered Jan 06 '23 20:01

Vamsi