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.

7 Comments

  1. sksrabon said:

    Nic tutorial… Thank u.

    October 13, 2013
    Reply
  2. Bhaskar said:

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

    December 18, 2013
    Reply
    • Me said:

      Hi, in the Toolbox make sure you have “All Windows Forms” expanded and you should see the timer control near the bottom.

      December 18, 2013
      Reply
  3. Ashwini said:

    thank u so much……

    March 31, 2015
    Reply
  4. unnamed said:

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

    July 22, 2015
    Reply
    • Select all images in the folder (ctrl+a)
      Now press F2 (function key)
      Type the name (Logo1) and press enter.

      Your problem will be solved.

      otherwise install a bulk renamer software.

      October 19, 2017
      Reply

Leave a Reply