C# 逐个导入并显示图像

C# 逐个导入并显示图像,c#,winforms,C#,Winforms,我有一个C#窗口表单,用于导入和显示多个图像 我可以导入多个图像并显示第一个图像,但在逐个显示图像时遇到一些问题 程序流程:用户单击按钮,然后选择多个图像。之后,第一个图像应显示在图片框上。当用户单击“下一个图像按钮”时,应显示下一个图像 第一个图像可以显示在picturebox上,但不知道如何逐个显示。 是否有任何配置可以实现这一点,或者如何通过编码实现这一点。 谢谢大家 我的编码: public partial class Form2 : Form { public Form2()

我有一个C#窗口表单,用于导入和显示多个图像

我可以导入多个图像并显示第一个图像,但在逐个显示图像时遇到一些问题

程序流程:用户单击按钮,然后选择多个图像。之后,第一个图像应显示在图片框上。当用户单击“下一个图像按钮”时,应显示下一个图像

第一个图像可以显示在picturebox上,但不知道如何逐个显示。 是否有任何配置可以实现这一点,或者如何通过编码实现这一点。 谢谢大家

我的编码:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        InitializeOpenFileDialog();
    }

    private void InitializeOpenFileDialog()
    {
        // Set the file dialog to filter for graphics files. 
        this.openFileDialog1.Filter =
            "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
            "All files (*.*)|*.*";

        // Allow the user to select multiple images. 
        this.openFileDialog1.Multiselect = true;
        this.openFileDialog1.Title = "My Image Browser";
    }

    private void SelectFileButton_Click(object sender, EventArgs e)
    {
        DialogResult dr = this.openFileDialog1.ShowDialog();
        if (dr == System.Windows.Forms.DialogResult.OK)
        {
            // Read the files 
            foreach (String file in openFileDialog1.FileNames)
            {
                // Create a PictureBox. 
                    PictureBox pb = new PictureBox();
                    Image loadedImage = Image.FromFile(file);
                    pb.Height = loadedImage.Height;
                    pb.Width = loadedImage.Width;
                    pb.Image = loadedImage;
                    flowLayoutPanel1.Controls.Add(pb);
            }

        }
    }
}

嗯,有更好的方法来实现这一点

您不必为每个图像添加
PictureBox
控件,它会使表单过载。 我的建议是保留所有加载图像的列表,以及当前显示图像的索引器

代码:

在表单中添加一个
PictureBox
(我们称之为
pictureBox1
),您希望在其中显示图像

此外,将以下属性添加到类中:

private List<Image> loadedImages = new List<Image>();
private int currentImageIndex;
最后,对于“下一步/上一步”按钮,单击“事件”,您可以添加以下代码:

// Mod function to support negative values (for the back button).
int mod(int a, int b)
{
    return (a % b + b) % b;
}

// Show the next picture in the PictureBox.
private void button_next_Click(object sender, EventArgs e)
{
    currentImageIndex = mod(currentImageIndex + 1, loadedImages.Count);
    pictureBox1.Image = loadedImages[currentImageIndex];
}

// Show the previous picture in the PictureBox.
private void button_prev_Click(object sender, EventArgs e)
{
    currentImageIndex = mod(currentImageIndex - 1, loadedImages.Count);
    pictureBox1.Image = loadedImages[currentImageIndex];
}

通过在SelectFileButton\u Click eventhandler中插入一个try catch,您可以看到问题的症结所在,请参阅:
// Mod function to support negative values (for the back button).
int mod(int a, int b)
{
    return (a % b + b) % b;
}

// Show the next picture in the PictureBox.
private void button_next_Click(object sender, EventArgs e)
{
    currentImageIndex = mod(currentImageIndex + 1, loadedImages.Count);
    pictureBox1.Image = loadedImages[currentImageIndex];
}

// Show the previous picture in the PictureBox.
private void button_prev_Click(object sender, EventArgs e)
{
    currentImageIndex = mod(currentImageIndex - 1, loadedImages.Count);
    pictureBox1.Image = loadedImages[currentImageIndex];
}