How to show images from folder into Picture box?
In this article I am going to explain about how to show all images from a specified folder with previous and next button in windows application. Here I have stored lots of pictures in the folder user view all images in the picture box.
Description :
I have stored lots of pictures in the folder user view all images in the picture box . Providing Previous and Next options to see previous and Next images in the same folder. Design side
I have design the form with two buttons Previous and Next look like below
Server side
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsFormsApplication15
{
public partial class Form1 : Form
{
ArrayList alist = new ArrayList();
int i = 0;
int filecount = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Specify Source folder path here
System.IO.DirectoryInfo inputDir = new System.IO.DirectoryInfo("D:\\images");
try
{
if ((inputDir.Exists))
{
//Get Each jpg files from a source folder and stored in the arraylist
System.IO.FileInfo file = null;
foreach (System.IO.FileInfo eachfile in inputDir.GetFiles())
{
file = eachfile;
if (file.Extension.ToLower() == ".jpg")
{
alist.Add(file.FullName);
//Store file count here
filecount = filecount + 1;
}
}
//Initally show first image in the picture box
pictureBox1.Image = Image.FromFile(alist[0].ToString()); ;
i = 0;
}
}
catch (Exception ex)
{
}
}
private void btnNext_Click(object sender, EventArgs e)
{
//check next postion image is have or not
if (i + 1 < filecount)
{
pictureBox1.Image = Image.FromFile(alist[i + 1].ToString()) ;
i = i + 1;
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
//check previous postion image is have or not
if (i - 1 >= 0)
{
pictureBox1.Image = Image.FromFile(alist[i-1].ToString()) ;
i = i - 1 ;
}
}
}
}Output :
Source code:
Client Side: Form Design
Code Behind: C#Conclusion
I hope this code snippet is help you to know about Display image from folder in picture box.
Thankyou..