简体   繁体   中英

Display many picture on ten pictureboxes one at a time

My program has ten pictures which show images from a folder i want. Adding a next button and previous button allow user to browse next ten pictures or last ten pictures. First ten pictures are shown successfully, but the next button only work when the folder has exactly twenty pictures ,if the pictures are fifteen it will crash. Here is my code:

PictureBox[] myPicBoxArray = new PictureBox[10];
string path = @"\\Documents\Pictures\Camera";

private void Form1_Load(object sender, EventArgs e)
{
    myPicBoxArray[0] = pictureBox1;
    myPicBoxArray[1] = pictureBox2;
    myPicBoxArray[2] = pictureBox3;
    myPicBoxArray[3] = pictureBox4;
    myPicBoxArray[4] = pictureBox5;
    myPicBoxArray[5] = pictureBox6;
    myPicBoxArray[6] = pictureBox7;
    myPicBoxArray[7] = pictureBox8;
    myPicBoxArray[8] = pictureBox9;
    myPicBoxArray[9] = pictureBox10;
}

//Show button to display first ten pictures
private void showButton_Click(object sender, EventArgs e)
{
    string[] files = Directory.GetFiles(path);
    int i = 0;
    foreach (string ofile in files)
    {
        myPicBoxArray[i].SizeMode = PictureBoxSizeMode.StretchImage;
        myPicBoxArray[i].Image = Image.FromFile(files[i]);

        i++;
        if (i >= 10)
            break;
    }
}

private void nextButton_Click(object sender, EventArgs e)//The problem is here,
{
    DirectoryInfo fileDir = new DirectoryInfo(path);
    while (i2 < 10)
    {
        myPicBoxArray[i2].SizeMode = PictureBoxSizeMode.StretchImage;
        if (i2 + 10 < picArrFileNames.Length)
        {
            myPicBoxArray[i2].Image = Image.FromFile(picArrFileNames[i2 + 10]);
        }
    }
}

No clue for the previous button.

Instead of using a limited array, you can simply store those images in ImageList and use the property myImageList.Images.Count to count the number of images in the ImageList.
problem solved.. :) or else go with Lists and use myList.Count .

You can do something like this:

   DirectoryInfo dir = new DirectoryInfo(filePath);
   foreach (FileInfo file in dir.GetFiles())
   {               
       this.myImageList.Images.Add(Image.FromFile(file.FullName));
   }

I have no idea why you are douing this :

if (i2 + 10 < picArrFileNames.Length)
  {
      myPicBoxArray[i2].Image = Image.FromFile(picArrFileNames[i2 + 10]);
  }

However,try this for your count issue,Also do a file extension check to avoid system files as adviced by MrGreen below.

private void nextButton_Click(object sender, EventArgs e)//The problem is here,
    {
        DirectoryInfo fileDir = new DirectoryInfo(path);
        int count = fileDir.GetFiles().Length;            
        while (i2 < count)
        {
            myPicBoxArray[i2].SizeMode = PictureBoxSizeMode.StretchImage;
            if (i2 + 10 < picArrFileNames.Length)
            {
                myPicBoxArray[i2].Image = Image.FromFile(picArrFileNames[i2 + 10]);
            }                                               
        }            
    }

Use this in a loop in here somewhere for extension checking Use this in a loop in here for extension checking

string e = Path.GetExtension("YourFilePathHere");
if (e == ".jpeg")
{
   //Do your stuff
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM