c# - 一次在十个图片框上显示多张图片

标签 c# .net winforms image picturebox

我的程序有十张图片,显示我想要的文件夹中的图片。添加下一个按钮和上一个按钮允许用户浏览下十张图片或最后十张图片。前 10 张图片显示成功,但是 next 按钮只有当文件夹刚好有 20 张图片时才有效,如果图片是 15 张就会崩溃。 这是我的代码:

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]);
        }
    }
}

没有上一个按钮的线索。

最佳答案

您可以简单地将这些图像存储在 ImageList 中,而不是使用有限的数组。并使用属性 myImageList.Images.Count 来计算 ImageList 中的图像数量。
问题解决了.. :) 或者使用 Lists 并使用 myList.Count

你可以这样做:

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

关于c# - 一次在十个图片框上显示多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13372491/

相关文章:

c# - WPF 中的每像素行进 Ant 选择边框

c# - 如何在代码中找到单元测试必须覆盖的地方

c# - 针对 Compact Framework 的 StackForm MSDN 建议的实际实现

.net - 在 Raspberry Pi 上调试 .NET 应用程序 - dotnet 已退出,代码为 0

c# - 检查表单是否具有焦点或处于事件状态

c# - 从用户控件打开 winform 并将值传回给用户控件

c# - OrderBy 抛出无法加载文件或程序集 System.Data.Entity

c# - Nlog 日志记录方法

c# - 从使用 Excel-DNA 构建的 Excel .XLL 文件中解压内容

c# - 具有覆盖 : Map an unmapped base class collection 的 Fluent Nhibernate Automapping