我正在尝试在列表视图中选择上一个图像并将其显示在右侧的图片框中。我正在使用_imageIndex来跟踪当前图片框中的内容,如果它小于图像列表长度,那么我在列表视图中选择图像并使用_imageIndex计数器来访问图像。
但它就像它根本没有获得图像索引。它只是在0处读取它并将其递减到负1并且当我再次单击btnPrevious时返回错误。
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);
}
}
}
答案 0 :(得分:0)
首先,您没有在任何地方设置_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。
希望这就是你要找的东西:)
致以最诚挚的问候,