首页 > 解决方案 > C#循环浏览图像列表并在图片框中显示一张图像

问题描述

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

如果是,则在列表框中选择下一个图像(包含图像列表)并将其显示在图片框中。我不知道该怎么做。这是代码。为了清楚起见,错误出现在 btnNext_Click 的第二个 if 语句中。错误说“运算符'>'不能应用于'int'和'Image'类型的操作数” GUI在运行时

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

标签: c#imagelistviewpictureboximagelist

解决方案


编辑:答案主要根据您的评论而改变。您将需要创建一个新成员变量来保存当前图像索引并确保用于访问 ImageList

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

int _imageIndex;

mnuOpen_Click调用后将此行添加到您的函数loadImageList();

_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++;
        }
    }

推荐阅读