C#循环浏览图像列表并在图片框中显示一张图像

C# Cycling through image list and displaying one image in the picturebox

我在按下一步按钮并在图片框中显示 selected 图像时循环浏览图像列表时遇到问题。我想在我的代码中做的是让用户单击 btnNext 并检查图片框中的图像值是否小于图像列表中的值。

如果是,则 select 列表框(包含图像列表)中的下一张图像并将其显示在图片框中。不过,我不确定该怎么做。这是代码。需要明确的是,错误出现在 btnNext_Click 的第二个 if 语句中。错误说 "Operator '>' cannot be applied to operands of type 'int' and 'Image'" GUI at runtime

string _big_fileName;
int _counter = 0;

public Form1()
    {            
        InitializeComponent();
    }

    //Displays larger instance of selected image in picture box.
    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //FOR i is less than the first image.
        for (int i = 0; i < listView1.SelectedItems.Count;i++)
        {                
            //GET filename from listview and store in index.
            _big_fileName = listView1.SelectedItems[i].Text;
            //Create larger instance of image.
            pictureBox1.Image = Image.FromFile(_big_fileName);
            //Fill panel to the width and height of picture box.
            panel1.AutoScrollMinSize = new Size(pictureBox1.Image.Width, 
                                                pictureBox1.Image.Height);
        }
    }

private void mnuOpen_Click(object sender, EventArgs e)
    {
        loadImageList();
    }

private void btnNext_Click(object sender, EventArgs e)
    {
        if(pictureBox1.Image != null)
        {
            //IF Image is less than Image list size.
            if (pictureBox1.Image < imageList1.Images.Count)
            {
                //ACCESS Imagelist size against image value
                _big_fileName = listView1.SelectedItems[_counter].Text;
                //INCREMENT Image list current position.
                _counter++;
                //ASSIGN current position to image.
                pictureBox1.Image = Image.FromFile(_big_fileName);
                //DISPLAY and enlarge image.
                panel1.AutoScrollMinSize = new Size(pictureBox1.Image.Width,
                                                pictureBox1.Image.Height);
            }
        }

        else
        {
            MessageBox.Show("No image.");
        }
    }

private void loadImageList()
    {
        imageList1.Images.Clear();
        listView1.Clear();

        oFD1.InitialDirectory = "C:\";
        oFD1.Title = "Open an Image File";
        oFD1.Filter = "JPEGS|*.jpg|GIFS|*.gif|PNGS|*.png|BMPS|*.bmp";

        //Open Dialog Box.
        var oldResults = oFD1.ShowDialog();

        if (oldResults == DialogResult.Cancel)
        {
            return;
        }

        try
        {
            //GET amount of filenames.
            int num_of_files = oFD1.FileNames.Length;
            //Store filenames in string array.
            string[] arryFilePaths = new string[num_of_files];


            //FOREACH filename in the file.
            foreach (string single_file in oFD1.FileNames)
            {
                //ACCESS array using _counter to find file.
                arryFilePaths[_counter] = single_file;
                //CREATE image in memory and add image to image list.
                imageList1.Images.Add(Image.FromFile(single_file));

                _counter++;
            }
            //BIND image list to listview.
            listView1.LargeImageList = imageList1;


            for (int i = 0; i < _counter; i++)
            {
                //DISPLAY filename and image from image index param. 
                listView1.Items.Add(arryFilePaths[i], i);
            }
        }

        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex.Message);
        }
    }

编辑:答案大部分已根据您的评论进行了更改。您将需要创建一个新的成员变量来保存当前图像索引并确保它用于访问 ImageList

创建一个成员变量来保存当前图像索引:

int _imageIndex;

在调用 loadImageList();

之后将此行添加到您的 mnuOpen_Click 函数
_imageIndex = 0;

在函数 btnNext_Click 中进行更改:

if (pictureBox1.Image < imageList1.Images.Count)

if (_imageIndex < imageList1.Images.Count)

_big_fileName = listView1.SelectedItems[_counter].Text;

_big_fileName = listView1.SelectedItems[_imageIndex].Text;

并添加行

_imageIndex++;

重新设计 btnNext_Click 事件处理程序

    private void btnNext_Click(object sender, EventArgs e)
    {
        //IF Image is less than Image list size.
        if (_imageIndex < imageList1.Images.Count)
        {
            listView1.Items[_imageIndex].Selected = true;

            _imageIndex++;
        }
    }