下面是看别人写的一段程序,就是打开一个文件夹,把里面的图片显示到listview中。
可是listview显示的图片中,有些图片和名称对应不起来,,,还有一个文件只有名称,没有图片
换过几个文件夹,结果都一样,是不是程序有问题,但是我还调试不出来。
大家帮忙private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.listView1.Clear();
                this.imageList1.Images.Clear();
                 
                 PPath = "C:\\abc";
                DirectoryInfo DInfo = new DirectoryInfo(PPath);
                FileSystemInfo[] FSInfo = DInfo.GetFileSystemInfos();
                for (int i = 0; i < FSInfo.Length; i++)
                {
                    string str = FSInfo[i].ToString();
                    string FileType = FSInfo[i].ToString().Substring(FSInfo[i].ToString().LastIndexOf(".") + 1, (FSInfo[i].ToString().Length - FSInfo[i].ToString().LastIndexOf(".") - 1));
                    FileType = FileType.ToLower();
                    if (FileType == "jpg" || FileType == "png" || FileType == "bmp" || FileType == "gif" || FileType == "jpeg")
                    {
                        string str1 = PPath + "\\" + str;
                        Image imageFile = Image.FromFile(str1);
                        this.imageList1.Images.Add(imageFile);  
                        this.listView1.Items.Add(FSInfo[i].ToString(),i);
                        imageFile.Dispose();
                       }
            }
}
}

解决方案 »

  1.   

    我是这么写的,dataGridView2是我暂存数据的,不显示出来,缩略图可以显示出来的。
    public void bindPic()
    {
        listView1.Items.Clear();
        imageList1.Images.Clear();
        for (int i = 0; i < dataGridView2.Rows.Count; i++)
        {
             CheckForIllegalCrossThreadCalls = false;
             string image = dataGridView2.Rows[i].Cells["图片路径"].Value.ToString();
             if (image == "")
                 imageList1.Images.Add(Image.FromFile(Application.StartupPath + "\\Image\\" + "2011839435593.jpg"));
             else
                 imageList1.Images.Add(Image.FromFile(Application.StartupPath + "\\Image\\" + image));
              ListViewItem lvi = new ListViewItem();
              lvi.Text = dataGridView2.Rows[i].Cells["物品名称"].Value.ToString() + "\n温度上限:" + dataGridView2.Rows[i].Cells["上限"].Value.ToString() + "℃" + "\n下限:" + dataGridView2.Rows[i].Cells["下限"].Value.ToString() + "℃";         //图片名称
               lvi.ImageIndex = i;   //这里就是你Listview每项显示的图片
               lvi.Tag = dataGridView2.Rows[i].Cells["id"].Value.ToString();  //这里可以绑定不显示的数据
                listView1.Items.Add(lvi);
          }
    }