下面是看别人写的一段程序,就是打开一个文件夹,把里面的图片显示到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();
}
}
}
}
可是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();
}
}
}
}
解决方案 »
- c#中用SharpPcap工具包抓到网络包是如何显示该包是何种协议类型
- C#中数据库连接问题
- 关于发布软件时注册的问题
- C# listview删除+窗体关闭问题~
- 读取txt格式文件内容,CPU占用率100%。请问如何解决?
- Oracle与C#:修改数据库,出现错误 急急急!!
- 怎样做一个类似淘宝旺旺的功能,在客户端登陆后,在我的淘宝中点击相应内容,可以直接打开相应网页,而无需在网页上再次登陆
- 光标文件怎样放入资源文件中,又怎样读取,为什么我读出来的是一块黑色的矩型?
- 我想在exe文件中保存程序状态,请大家帮忙
- 微软专家帮忙:从数据库导入Excel时中文出现乱码?
- 跪求印章解决方案
- 请高手们说说Task和Thread的区别
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);
}
}