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

时间:2018-05-22 06:18:39

标签: c# image listview picturebox imagelist

按下下一个按钮并在图片框中显示所选图像,我在浏览图像列表时出现问题。我想在我的代码中做的是让用户点击btnNext并检查图片框中的图像值是否小于图像列表中的图像值。

如果是,则选择列表框中的下一个图像(包含图像列表)并将其显示在图片框中。我不知道如何做到这一切。这是代码。只是要明确错误是在btnNext_Click的第二个if语句中。错误说"运算符''不能应用于' int'类型的操作数和'图像" 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);
        }
    }

1 个答案:

答案 0 :(得分:2)

编辑:答案主要根据您的评论进行更改。您需要创建一个新的成员变量来保存当前图像索引并确保它用于访问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++;
        }
    }