In a project I want my picturebox control to load more than one image so that I can view the images by navigating Next/Back buttons in C# windows form application databnase.
Please can someone help me?
In a project I want my picturebox control to load more than one image so that I can view the images by navigating Next/Back buttons in C# windows form application databnase.
Please can someone help me?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Bhuvanesh MohankumarPosted Jun 27, 2025, 5:11 PM
Here's a step-by-step breakdown of how to implement this:
1. Set up your form:
PictureBoxcontrol to your form.Buttoncontrols, one for "Next" and one for "Back".Labelcontrol to display the current image index (optional).2. Declare variables:
images = new List();
private List
private int currentImageIndex = 0;
Load images into the collection:
private void LoadImagesFromPaths(string[] filePaths)
{
foreach (string filePath in filePaths)
{
try
{
images.Add(Image.FromFile(filePath));
}
catch (Exception ex)
{
MessageBox.Show($"Error loading image: {ex.Message}");
}
}
DisplayImage(); // Display the first image
}
Example loading from resources.
private void LoadImagesFromResources()
{
images.Add(Properties.Resources.Image1);
images.Add(Properties.Resources.Image2);
// Add more images as needed
DisplayImage();
}
4. Display the image: Use PictureBox
private void DisplayImage()
{
if (images.Count > 0)
{
pictureBox1.Image = images[currentImageIndex];
label1.Text = $"Image {currentImageIndex + 1} of {images.Count}"; // Update label
}
else
{
pictureBox1.Image = null; // Or a default image
label1.Text = "No images loaded";
}
}
5. Implement Next/Back button logic:
private void NextButton_Click(object sender, EventArgs e)
{
if (images.Count > 0)
{
currentImageIndex = (currentImageIndex + 1) % images.Count; // Wrap around
DisplayImage();
}
}
Use similar code ofr Back button as well.