Use a loop to load pictures into PictureBoxes in C#

When the form loads, it builds the path to the image files. It then creates an array holding references to the form's PictureBoxes. (If you use this array in more than once place, you can declare it at a module level so you don't need to recreate it later.)

The program then loops through the items in the array, loading the corresponding pictures named pic0.png, pic1.png, and so forth.

private void Form1_Load(object sender, EventArgs e)
{
string dirname = Path.GetFullPath(
Path.Combine(Application.StartupPath, @"..\..\"));

// Make an array holding the PictureBoxes.
PictureBox[] pics = { PictureBox1, PictureBox2, PictureBox3, PictureBox4 };

// Load the pictures in a loop.
for (int i = 0; i < pics.Length; i++)
{
pics[i].Image = new Bitmap(dirname + "pic" + i.ToString() + ".png");
}
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.