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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Bhuvanesh MohankumarRank #310 · 8.1K reputationPosted Jun 27, 2025, 1:11 PM EDT
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.