C# Tutorial – Automatically Load Picture Files from a Folder and Display in Slide Show

In this tutorial we will make a automatic slide show app. This slide show will start by asking the user to browse folder. the user can pick a folder that they want to view the pictures from. Once you select the folder, the program will scan through the folder to store the names of each image file. For this program we will only look for image files such as JPG, GIF, PNG and Bitmap images. Other files such as documents, music and videos will be ignore. All of the filtered files will be saved in a list and then the program will start the slideshow by going through the pictures one by one. We also added a small feature for the user to pick the speed of the playback. So they can pick from 1x,  2x or 3x times the speed for the slide show. The major benefit of this is tutorial is that it allows to make something that we can use ourselves and show you that sometimes its best to make things yourself, this can be a form of problem solving because you can treat yourself like the target user for your designs. I had a lot of fun making this tutorial and hopefully you enjoy it.

Lesson Objectives –

  1. Make a automatic slide show app using windows form application in visual studio
  2. Load picture files from a folder and store them in a list
  3. Load one item at a time from that list and show it on the picture box
  4. Allow users to change speed of the slide show

Video Tutorial

Download The Automatic Image Slide Show Project Tutorial on GitHub

 

Full Source Code –

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO; // this one is needed for reading external files

namespace Automatic_Folder_Image_Slide_Show
{
    public partial class Form1 : Form
    {

        List<string> filteredFiles;
        FolderBrowserDialog FolderBrowser = new FolderBrowserDialog();

        int counter = -1;
        int timerInterval = 1000;
        bool isPlaying = false;

        public Form1()
        {
            InitializeComponent();

            radioButton1.Checked = true;
            slideShowTimer.Interval = timerInterval;

        }

        private void BrowseForImages(object sender, EventArgs e)
        {

            counter = -1;
            isPlaying = false;
            slideShowTimer.Stop();
            btnPlay.Text = "Play";

            DialogResult result = FolderBrowser.ShowDialog();

            filteredFiles = Directory.GetFiles(FolderBrowser.SelectedPath, "*.*")
                .Where(file => file.ToLower().EndsWith("jpg") || file.ToLower().EndsWith("gif")
                || file.ToLower().EndsWith("png") || file.ToLower().EndsWith("bmp")).ToList();

            lblFileInfo.Text = "Folder loaded - Now Press Play!";

        }

        private void PlayStopSlideShow(object sender, EventArgs e)
        {
            if (isPlaying == false)
            {
                btnPlay.Text = "Stop";
                slideShowTimer.Start();
                isPlaying = true;
            }
            else
            {
                btnPlay.Text = "Play";
                isPlaying = false;
                slideShowTimer.Stop();
            }
        }

        private void PlaySlideShowTimerEvent(object sender, EventArgs e)
        {
            counter += 1;

            if (counter >= filteredFiles.Count)
            {
                counter = -1;
            }
            else
            {
                imageViewer.Image = Image.FromFile(filteredFiles[counter]);
                lblFileInfo.Text = filteredFiles[counter].ToString();
            }

        }

        private void SpeedChangeEvent(object sender, EventArgs e)
        {

            RadioButton tempRadioButton = sender as RadioButton;

            switch (tempRadioButton.Text.ToString())
            {
                case "1x":
                    timerInterval = 3000;
                    break;
                case "2x":
                    timerInterval = 2000;
                    break;
                case "3x":
                    timerInterval = 1000;
                    break;
            }

            slideShowTimer.Interval = timerInterval;

        }
    }
}

 




Comments are closed.