急!C#怎么将listview和imagelist结合显示一个文件夹里的图片?通过点击图片在另一个窗口显示放大图片?
展开全部
private void SearchImageInFolder(string FolderPath)
{
imageList1.Images.Clear();
listView1.Items.Clear();
string[] strImageTypes = new string[4] { "*.gif", "*.png", "*.jpg", "*.bmp" };
foreach (string strImageType in strImageTypes)
{
string[] strImages = Directory.GetFiles(FolderPath, strImageType, SearchOption.AllDirectories);
foreach (string strImg in strImages)
{
Image img = Image.FromFile(strImg);
string strFileName = Path.GetFileName(strImg);
imageList1.Images.Add(strFileName, img);
listView1.Items.Add(new ListViewItem(strImg, strFileName));
}
}
}
private void btnOpenImage_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlgOpen = new FolderBrowserDialog();
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
SearchImageInFolder(dlgOpen.SelectedPath);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
return;
}
string strImageFile = listView1.SelectedItems[0].Text;
Image img = Image.FromFile(strImageFile);
pictureBox1.Image = img;
}
SearchImageInFolder 根据输入的文件夹路径查询图片,绑定到ImageList,并在ListView中显示。
btnOpenImage_Click 打开文件夹
listView1_SelectedIndexChanged 根据ListView里选择不同的Item将图片显示在PictureBox中,若是在另一个窗口显示可以把文件路径或Image对象作为参数传递。
{
imageList1.Images.Clear();
listView1.Items.Clear();
string[] strImageTypes = new string[4] { "*.gif", "*.png", "*.jpg", "*.bmp" };
foreach (string strImageType in strImageTypes)
{
string[] strImages = Directory.GetFiles(FolderPath, strImageType, SearchOption.AllDirectories);
foreach (string strImg in strImages)
{
Image img = Image.FromFile(strImg);
string strFileName = Path.GetFileName(strImg);
imageList1.Images.Add(strFileName, img);
listView1.Items.Add(new ListViewItem(strImg, strFileName));
}
}
}
private void btnOpenImage_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlgOpen = new FolderBrowserDialog();
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
SearchImageInFolder(dlgOpen.SelectedPath);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
return;
}
string strImageFile = listView1.SelectedItems[0].Text;
Image img = Image.FromFile(strImageFile);
pictureBox1.Image = img;
}
SearchImageInFolder 根据输入的文件夹路径查询图片,绑定到ImageList,并在ListView中显示。
btnOpenImage_Click 打开文件夹
listView1_SelectedIndexChanged 根据ListView里选择不同的Item将图片显示在PictureBox中,若是在另一个窗口显示可以把文件路径或Image对象作为参数传递。
追问
能说下具体操作步骤吗?看的不是很懂啊
追答
这些是写在一个窗体里的,画面上放一个Button、ListView、ImageList、PictureBox
Button名字:btnOpenImage
Button的Click事件就是btnOpenImage_Click,里面调用了SearchImageInFolder,
listView1_SelectedIndexChanged是将选择的图片显示在PictureBox里。
SearchImageInFolder函数里就是读取一个文件夹里的所有图片(包含了子文件夹:SearchOption.AllDirectories),将读取到的图片添加到ImageList里,并且为ListView添加Item同时指定图片。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询