简体   繁体   English

一次一个地在十个图片框上显示许多图片

[英]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. 您可以简单地将这些图像存储在ImageList并使用属性myImageList.Images.Count来计算ImageList中的图像数量,而不是使用有限的数组。
problem solved.. :) or else go with Lists and use myList.Count . 问题解决了.. :)或者使用Lists并使用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. 但是,请尝试使用此计数器,同时执行文件扩展名检查以避免系统文件,如下面的MrGreen所建议的那样。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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