如何在列表视图中显示的图像下显示filename
本文关键字:显示 图像 filename 列表 视图 | 更新日期: 2023-09-27 18:20:44
我正在编写一个代码,其中我需要在列表视图中显示多个图像,我可以显示图像,但如何在图像下方显示文件名?
下面是我的代码的一部分
string stu_name_1 = listBox4.SelectedItem.ToString();
string stu_name_2 = listBox6.SelectedItem.ToString();
string add = stu_name_1 +"/"+ stu_name_2;
Directory.CreateDirectory(add);
OpenFileDialog sf = new OpenFileDialog();
Dictionary<string, string> imageDictionary = new Dictionary<string, string>();
sf.Multiselect = true;
if (sf.ShowDialog() == DialogResult.OK)
{
string[] files = sf.FileNames;
foreach (string file in files)
{
// Use static Path methods to extract only the file name from the path.
string fileName = System.IO.Path.GetFileName(file);
string destFile = System.IO.Path.Combine(add, fileName);
Image imgToAdd = Image.FromFile(file);
imgToAdd.Tag = file;
System.IO.File.Copy(file, destFile, true);
imageList1.Images.Add(imgToAdd);
//imageDictionary.Add(Path.GetFileName(file), file);
}
}
listView1.Items.Clear();
for (int i = 0; i < imageList1.Images.Count; i++)
{
ListViewItem lvi = new ListViewItem();
lvi.ImageIndex = i;
listView1.Items.Add(lvi);
string stu_name_1 = listBox4.SelectedItem.ToString();
string stu_name_2 = listBox6.SelectedItem.ToString();
string add = stu_name_1 +"/"+ stu_name_2;
Directory.CreateDirectory(add);
OpenFileDialog sf = new OpenFileDialog();
Dictionary<string, string> imageDictionary = new Dictionary<string, string>();
sf.Multiselect = true;
if (sf.ShowDialog() == DialogResult.OK)
{
listView1.Items.Clear();//Clear first
string[] files = sf.FileNames;
foreach (string file in files)
{
// Use static Path methods to extract only the file name from the path.
string fileName = System.IO.Path.GetFileName(file);
string destFile = System.IO.Path.Combine(add, fileName);
Image imgToAdd = Image.FromFile(file);
imgToAdd.Tag = file;
System.IO.File.Copy(file, destFile, true);
imageList1.Images.Add(imgToAdd);//Suppose the imageList1 is dedicated to your listView1 only.
listView1.Items.Add(fileName, imageList1.Images.Count-1);
//imageDictionary.Add(Path.GetFileName(file), file);
}
}