0

我正在编写一个代码,我需要在列表视图中显示几个图像,我可以获得显示的图像,但我怎么能在图像下方显示文件名?

以下是我的代码的一部分

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

1 回答 1

0
    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);
        }
    }
于 2013-07-03T00:31:47.853 回答