Click here to Skip to main content
5,788,961 members and growing! (20,728 online)
Email Password   helpLost your password?
Languages » C# » Applications     Beginner License: The Code Project Open License (CPOL)

A Simple Image Slide Show

By vidyaputra

A simple application that show how to code a simple image slide show for beginner
C# (C# 2.0, C#), .NET (.NET, .NET 2.0)

Posted: 21 Jan 2008
Updated: 21 Jan 2008
Views: 8,686
Bookmarked: 11 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 1.16 Rating: 1.67 out of 5
3 votes, 60.0%
1
1 vote, 20.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
1 vote, 20.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
imageviewer

Introduction

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

Using the code

To make this application I use 3 button (for the next, previous, open and slide show button), and then 1 picture box, and 1 panel (to contain the picture box). And the picture box “sizemode” is set to “StretchImage”.

First, declare 4 variable that will be used :

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

First variable is a string array that will be used to keep the path file of the folder. The second, third and fourth variable is used to ‘mark’ the beginning of the array, the end of the array and mark the selected index of the array.

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.Enabled = true;
}
}

If the folderBrowserDialog result is ‘OK’, then get all the jpg, jpeg and bmp files on the folder and then copy it to the fileFolder array. Then show the image by calling the 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 is devided with 2 to show the image twice smaller than the 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 image is simply by move the selected array mark to the next index or to previous index.

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 the nextImage() at the timer1_tick then just set the enabled into true or false.

Conclution

Its easy to open, show and slide show images. Just by using folderBrowserDialog and timer using C# 2.0.

I hope this article can be usefull for anyone. Any comment is welcome!

License

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

About the Author

vidyaputra



Occupation: Software Developer (Junior)
Location: Indonesia Indonesia

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralNice but...memberjinfrics20:39 28 Apr '08  
GeneralRe: Nice but...memberWenderson11:44 18 Jul '08  
GeneralhimemberRamzi Abou Rahal9:10 12 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Jan 2008
Editor:
Copyright 2008 by vidyaputra
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project