C# 返回listview中的上一张图片并显示在Picturebox中

C# Going back to previous image in listview and displaying it in Picturebox

我正在尝试select列表视图中的上一张图片,并显示在右侧的图片框中。我正在使用 _imageIndex 来跟踪当前图片框中的内容,如果它小于图像列表长度,那么我 select 列表视图中的图像并使用 _imageIndex 计数器访问图像。

但是好像根本就没有得到图片索引。当我再次单击 btnPrevious 时,它只是继续读取 0 并将其递减为负 1 和 return 错误。

System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '-1' is not valid for 'index'.
Parameter name: index'

我知道我在 mnuOpen_Click 事件中将它的 _imageIndex 设置为 0。除此之外,我不知道如何解决这个问题。为清楚起见,这里是 GUI 的图像。请出主意?

public partial class Form1 : Form
{
    string _big_fileName;
    int _counter = 0;
    int _imageIndex;

    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();
        _imageIndex = 0;
    }

    private void btnPrevious_Click(object sender, EventArgs e)
    {
        if (pictureBox1.Image != null)
        {                
            //IF Image is less than Image list size.
            if (_imageIndex < imageList1.Images.Count)
            {
                //SELECT  listview items with counter.
                listView1.Items[_imageIndex].Selected = true;
                //GO to previous entry.
                _imageIndex--;
            }
        }

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

    //Cycle to next image in image list and display in Picturebox.
    private void btnNext_Click(object sender, EventArgs e)
    {
        if(pictureBox1.Image != null)
        {
            //IF Image is less than Image list size.
            if (_imageIndex < imageList1.Images.Count)
            {
                listView1.Items[_imageIndex].Selected = true;

                _imageIndex++;
            }
        }

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

首先,您没有在任何地方设置 _imageIndex,除了 btnPrevious_Click,您将其设置为 0。

您需要在 SelectedIndexChanged 方法中将其设置为选定的索引,如下所示(注意添加到您之前代码的最后一行):

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);
            _imageIndex = listView1.SelectedIndices[0];
        }
    }

接下来,在 btnPreviousClick 中,您需要做一些更改:

private void btnPrevious_Click(object sender, EventArgs e)
    {
        if (pictureBox1.Image != null)
        {
            //IF Image is less than Image list size.
            if (_imageIndex >= 0 && _imageIndex < imageList1.Images.Count)
            {
                //De-select current item
                listView1.Items[_imageIndex].Selected = false;
                //GO to previous entry.
                _imageIndex--;
                //Select new item
                if(_imageIndex>=0){
                   listView1.Items[_imageIndex].Selected = true;
                   listView1.Select();
                }
            }
        }
        else
        {
            MessageBox.Show("No image.");
        }
    }

需要调用 listView1 的 Select() 方法。最大的问题是您没有设置 _imageIndex。

希望这就是您要找的:)

此致,