Image Slideshow on Windows Form C# with Timer Control

Quick example of how to display an image control that loops through a folder of images.

Firstly drop a timer control onto your form (name: tmrNextImage), set the Interval to 1000 milliseconds (1 second) and set enabled to true.

Next drop a PictureBox control onto your form (name: imgPicture) and set SizeMode to Zoom (this will force your image to fit the box), drag your box to any size you like.

Now we need some images. I copied images to a folder (C:\Data_Store\Images\) and named them Logo1.jpg, Logo2.jpg, etc up to Logo6.jpg

Now lets make the code to load the image into the PictureBox control. We need a variable to ‘remember’ our image count, in this case imageNumber. Our loadNextImage checks to see if we’ve reached 7 (one more than the number of images we have) and if so sets imageNumber back to 1.

We then set the ImageLocation to our C:\Data_store\images\ plus our image name with the imageNumber inserted into the filename, e.g. Logo1.jpg the first time round. And then increment imageNumber by 1.

        private int imageNumber = 1;

        private void loadNextImage()
        {
            if(imageNumber == 7)
            {
                imageNumber = 1;
            }
            imgPicture.ImageLocation = string.Format(@"C:\data_store\images\logo{0}.jpg",imageNumber);
            imageNumber++;
        }

So now we need to trigger the image to change. To do this we use the Tick event of the timer (which will happen every 1000 milliseconds as we set earlier). So each time the tick event happens we call the loadNextImage code.

        private void tmrNextImage_Tick(object sender, EventArgs e)
        {
            loadNextImage();
        }

Now when you open the form the image should change every second.

Advertisements
This entry was posted in Programming and tagged , , , , . Bookmark the permalink.

6 Responses to Image Slideshow on Windows Form C# with Timer Control

  1. sksrabon says:

    Nic tutorial… Thank u.

  2. Bhaskar says:

    how to drop the timer control in my c# form. there is no such control in my toolbox

  3. deenkadir says:

    Reblogged this on deenkadir's Blog.

  4. Ashwini says:

    thank u so much……

  5. unnamed says:

    Very helpful! But what if I don’t have 100 images and I cannot rename them one by one to Logo1, Logo2, Logo3…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s