Click here to Skip to main content
12,758,605 members (32,595 online)
Click here to Skip to main content
Add your own
alternative version

Stats

109.3K views
8K downloads
40 bookmarked
Posted 21 Jan 2008

A Simple Image Slide Show

, 21 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple image slide show for beginners.

imageviewer

Introduction

This is a simple C# application that can open and view images, and show the images one by one in a slide show. The purpose of this article is for a beginner to know how to create an image slide show using timers, in C#.

Using the code

To make this application, I used Buttons (for Next, Previous, Open, and Slideshow), a PictureBox, and a Panel (to contain the PictureBox). The PictureBoxSizeMode” is set to “StretchImage”.

First, declare these four variables:

private string [] folderFile = null;
private int selected = 0;
private int begin = 0;
private int end = 0;

The first variable is a string array that will be used to keep the path file of the folder. The remaining variables are used to ‘mark’ the beginning of the array, the end of the array, and the selected index of the array, respectively.

private void button2_Click(object sender, System.EventArgs e)
{
    if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
    {
        string [] part1=null, part2=null, part3=null;
        part1 = Directory.GetFiles(folderBrowserDialog1.SelectedPath,"*.jpg");
        part2 = Directory.GetFiles(folderBrowserDialog1.SelectedPath,"*.jpeg");
        part3 = Directory.GetFiles(folderBrowserDialog1.SelectedPath,"*.bmp");
        folderFile = new string[part1.Length + part2.Length + part3.Length];
        Array.Copy(part1,0,folderFile,0,part1.Length);
        Array.Copy(part2,0,folderFile,part1.Length,part2.Length);
        Array.Copy(part3,0,folderFile,part1.Length + part2.Length,part3.Length);
        selected = 0;
        begin = 0;
        end = folderFile.Length;
        showImage(folderFile[selected]);
        button1.Enabled = true;
        button3.Enabled = true;
        button4.Enab    led = true;
    }
}

If the FolderBrowserDialog result is ‘OK’, then get all the JPG, JPEG, and BMP files on the folder and copy them to the fileFolder array. Then, show the images by calling showImage().

private void showImage(string path)
{
    Image imgtemp = Image.FromFile(path);
    pictureBox1.Width = imgtemp.Width / 2;
    pictureBox1.Height = imgtemp.Height / 2;
    pictureBox1.Image = imgtemp;
}

The PictureBox width and height are divided with 2 to show the images half their original size.

private void prevImage()
{
    if(selected == 0)
    {
        selected = folderFile.Length - 1;
        showImage(folderFile[selected]); 
    }
    else
    {
        selected = selected - 1; showImage(folderFile[selected]);
    }
}

private void nextImage()
{
    if(selected == folderFile.Length - 1)
    {
        selected = 0; 
        showImage(folderFile[selected]);
    }
    else
    {
        selected = selected + 1; showImage(folderFile[selected]);
    }
}

To show the next and previous images, simply move the selected array mark to the next index or to previous index, respectively.

private void timer1_Tick(object sender, System.EventArgs e)
{ 
    nextImage();
}

private void button4_Click(object sender, System.EventArgs e)
{
    if(timer1.Enabled == true)
    { 
        timer1.Enabled = false;
        button4.Text = "<< START Slide Show >>";
    }
    else
    {
        timer1.Enabled = true;
        button4.Text = "<< STOP Slide Show >>";
    }
}

For the slide show function, call nextImage() at the timer1_tick, then just set Enabled to true or false.

Conclusion

It is very easy to open, show, and slide show images. We just need to use FolderBrowserDialog and Timer and C# 2.0.

I hope this article is useful for everyone. Any comments are welcome!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

vidyaputra
Software Developer (Junior)
Indonesia Indonesia
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 3 Pin
Member 1051355325-Mar-14 16:05
memberMember 1051355325-Mar-14 16:05 
QuestionThanks and a little thing Pin
Orions6820-Jul-13 11:43
memberOrions6820-Jul-13 11:43 
QuestionAn even simpler slideshow Pin
K Turner24-Jan-13 23:17
memberK Turner24-Jan-13 23:17 
Questiongood example Pin
jumput23-Jan-13 1:31
memberjumput23-Jan-13 1:31 
GeneralMy vote of 5 Pin
alcitect19-Nov-12 0:02
memberalcitect19-Nov-12 0:02 
GeneralMy vote of 3 Pin
rkmeenajaipur1-Sep-11 23:44
memberrkmeenajaipur1-Sep-11 23:44 
GeneralMy vote of 1 Pin
jalil200830-May-11 6:51
memberjalil200830-May-11 6:51 
GeneralThanks Pin
rajamahan25-Jun-10 9:30
memberrajamahan25-Jun-10 9:30 
GeneralThankS! Pin
rajamahan25-Jun-10 9:21
memberrajamahan25-Jun-10 9:21 
GeneralNice but... Pin
jinfrics28-Apr-08 20:39
memberjinfrics28-Apr-08 20:39 
GeneralRe: Nice but... Pin
Wenderson18-Jul-08 11:44
memberWenderson18-Jul-08 11:44 
GeneralRe: Nice but... Pin
Grimrist31-Jan-09 13:05
memberGrimrist31-Jan-09 13:05 
GeneralRe: Nice but... Pin
vidyaputra2-Feb-09 4:21
membervidyaputra2-Feb-09 4:21 
Generalhi Pin
Ramzi Abou Rahal12-Mar-08 9:10
memberRamzi Abou Rahal12-Mar-08 9:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 21 Jan 2008
Article Copyright 2008 by vidyaputra
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid