using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace QianMingForm
{
public partial class Photo : Form
{
private List<string> tifNames = new List<string>();
private int index;
public Photo()
{
InitializeComponent();
}
private void Photo_Shown(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
string filename = string.Format(@"{0}\Data\", Environment.CurrentDirectory);
DirectoryInfo TheFolder = new DirectoryInfo(filename);//文件路径
for (int i = 0; i < TheFolder.GetFiles().Length; i++)//遍历文件夹
{
if (TheFolder.GetFiles()[i].Length > 0 && TheFolder.GetFiles()[i].Extension == ".png")//或者jpg,png 文件大小要大于0且是图片文件
{
Image image = Image.FromFile(TheFolder.GetFiles()[i].DirectoryName + "\\" + TheFolder.GetFiles()[i].Name); //获取文件
tifNames.Add(TheFolder.GetFiles()[i].Name);//添加文件名
imageList2.Images.Add(image);//添加图片
}
}
//初始化设置
this.listView1.View = View.LargeIcon;
this.listView1.LargeImageList = this.imageList2;
//开始绑定
this.listView1.BeginUpdate();
for (int i = 0; i < tifNames.Count; i++)
{
ListViewItem lvi = new ListViewItem();
lvi.ImageIndex = i;
//lvi.Text = tifNames[i];//显示文件名
this.listView1.Items.Add(lvi);
}
this.listView1.EndUpdate();
}
private void but_close_Click(object sender, EventArgs e)
{
this.Close();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
string filename = string.Format(@"{0}\Data\", Environment.CurrentDirectory);
if (this.listView1.SelectedItems.Count == 0)
return;
index = this.listView1.SelectedItems[0].Index;
//MessageBox.Show(tifNames[index]);
this.pictureBox1.Image = Image.FromFile(filename + "\\" + tifNames[index]);
}
}
}
文件夹里只有3张图片,怎么显示出来两次
------解决思路----------------------
加断点 调试 看 tifNames.Count 是多少,不对就是那个地方的判断条件有问题 了。
------解决思路----------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace QianMingForm
{
public partial class Photo : Form
{
private List<string> tifNames = new List<string>();
private int index;
public Photo()
{
InitializeComponent();
}
private void Photo_Shown(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
string filename = string.Format(@"{0}\Data\", Environment.CurrentDirectory);
DirectoryInfo TheFolder = new DirectoryInfo(filename);//文件路径
for (int i = 0; i < TheFolder.GetFiles().Length; i++)//遍历文件夹
{
if (TheFolder.GetFiles()[i].Length > 0 && TheFolder.GetFiles()[i].Extension == ".png")//或者jpg,png 文件大小要大于0且是图片文件
{
Image image = Image.FromFile(TheFolder.GetFiles()[i].DirectoryName + "\\" + TheFolder.GetFiles()[i].Name); //获取文件
tifNames.Add(TheFolder.GetFiles()[i].Name);//添加文件名
imageList2.Images.Add(image);//添加图片
}
}
//初始化设置
Console.WriteLine(listView1.items.count);
this.listView1.View = View.LargeIcon;
this.listView1.LargeImageList = this.imageList2; 已经加载了
Console.WriteLine(listView1.items.count);
//开始绑定
this.listView1.BeginUpdate();
for (int i = 0; i < tifNames.Count; i++)
{
ListViewItem lvi = new ListViewItem();
lvi.ImageIndex = i;
//lvi.Text = tifNames[i];//显示文件名
Console.WriteLine(listView1.items.count);
this.listView1.Items.Add(lvi); 又加载一次
Console.WriteLine(listView1.items.count);
}
this.listView1.EndUpdate();
}
private void but_close_Click(object sender, EventArgs e)
{
this.Close();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
string filename = string.Format(@"{0}\Data\", Environment.CurrentDirectory);
if (this.listView1.SelectedItems.Count == 0)
return;
index = this.listView1.SelectedItems[0].Index;
//MessageBox.Show(tifNames[index]);
this.pictureBox1.Image = Image.FromFile(filename + "\\" + tifNames[index]);
}
}
}
添加些打试试,家里电脑没有装vs,测试不了,你自己也加些打印试试