c# - C# 在列表视图中返回上一个图像并将其显示在图片框中
问题描述
我正在尝试在列表视图中选择上一张图像并将其显示在右侧的图片框中。我正在使用 _imageIndex 来跟踪当前图片框中的内容,如果它小于图像列表长度,那么我在列表视图中选择图像并使用 _imageIndex 计数器访问图像。
但这就像它根本没有获得图像索引一样。当我再次单击 btnPrevious 时,它只是继续读取 0 并将其递减为负 1 并返回错误。
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。
希望这就是你要找的:)
最好的祝福,
推荐阅读
- react-native - 如何使 React 本机 Web 视图支持来自本地服务器的摄像头和自签名证书?
- performance - 为什么对我的网站的第一次请求很慢
- python - 在 Python 中实现变量等待循环
- c# - 为 Net Core 重建的 T4 模板生成器
- xpath - XPath 在字符串位于标签之间时加入字符串
- android - 如何在屏幕外定位 ConstraintLayout 的子项(以便稍后在屏幕上翻译它们)?
- node.js - 如何使用 webpack 4 成功构建带有 pug 模板引擎的 NodeJS 项目
- javascript - JS Cookie 根据用户显示/隐藏表单
- javascript - 使用 .reduce() 对 Javasript 数组进行从宽到长的转换会产生空数组
- angular - pathType.isDirectory 中的错误不是函数