怎么从文件名中找图片文件?

mohugomohu 2008-01-22 07:10:22
FolderBrowserDialog fbd=new FolderBrowserDialog();
if(fbd.ShowDialog()==DialogResult.OK)
{
DirectoryInfo di=(fbd.SelectPath);
FileInfo[] fileList=di.GetFiles;
ArrayList arr=new ArrayList();
foreach(FileInfo fi in fileList)
{
arr.Add(fi.FullName);
}
arrNames=(string[])arr.ToArray(typeof(string));
picMain.Image=Image.FromFile(arrNames[0]);
}

上面代码是从用户选择的文件夹中找出所有文件名,以显示图片.
怎么筛选出图片?(我只要 *.jpg,*.png,*.bmp等文件的文件名)
...全文
193 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
duping9626 2008-01-23
  • 打赏
  • 举报
回复
就算是提供了Filter属性又如何,你以为.NET会有多高明,还不是一样的遍历去匹配,只不过封装了而已,
用Regex的话性能更低,不信你试试
jimgreat 2008-01-23
  • 打赏
  • 举报
回复
用正责表达式

FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
DirectoryInfo di = new DirectoryInfo(fbd.SelectedPath);
FileInfo[] fileList = di.GetFiles();
ArrayList arr = new ArrayList();

Regex rg=new Regex(@"^.+\.bmp$|^.+\.png$|^.+\.jpg&");

foreach (FileInfo fi in fileList)
{
if(rg.IsMatch(fi.FullName))
arr.Add(fi.FullName);
}
string[] arrNames = (string[])arr.ToArray(typeof(string));
picMain.Image = Image.FromFile(arrNames[0]);
}
mohugomohu 2008-01-23
  • 打赏
  • 举报
回复
我已经用了openfiledialog类来打开文件了,现在我要做打开文件夹功能.
请问谁还有好的办法,拿出来分享啊!我觉得5楼的办法太麻烦了
sjm2003 2008-01-23
  • 打赏
  • 举报
回复
那不如就用openfiledialog
mohugomohu 2008-01-23
  • 打赏
  • 举报
回复
最终,我用了5楼的办法.因为正则表达式我不会用.
我觉得,既然C#的特色是封装对象,那就充分发挥它的特色.
如果不封装对象方法而直接写一堆代码的话,不好看
mohugomohu 2008-01-22
  • 打赏
  • 举报
回复
我觉得答案都不太好.
有没有象OpenFileDialog类的Filter那样直接把 *.bmp,*.png,*.jpg一起全部筛选的?
duping9626 2008-01-22
  • 打赏
  • 举报
回复

FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
ArrayList arr = new ArrayList();
foreach (string str in System.IO.Directory.GetFiles(fbd.SelectedPath, "*.jpg"))
{
arr.Add(str);
}
foreach (string str in System.IO.Directory.GetFiles(fbd.SelectedPath, "*.pnp"))
{
arr.Add(str);
}
foreach (string str in System.IO.Directory.GetFiles(fbd.SelectedPath, "*.bmp"))
{
arr.Add(str);
}

arrNames = (string[])arr.ToArray(typeof(string));
picMain.Image = Image.FromFile(arrNames[0]);
}
sjm2003 2008-01-22
  • 打赏
  • 举报
回复
if (fi.Name.Substring(fi.Name.LastIndexOf('.'),4).ToString()==".bmp")
{
..................................
}
这个可以判断最后的字母是不是.bmp||.jpg||.png

你的程序似乎有错误,我调试的时候是这样!

FolderBrowserDialog fbd=new FolderBrowserDialog();
if(fbd.ShowDialog()==DialogResult.OK)
{
DirectoryInfo di=(fbd.SelectPath); //这里怎么都说有错,除非改成new DirectoryInfo(fbd.selectPath);改了后,又好象没找到图象,不知道是不是我的文件夹冒图象!…^_^

FileInfo[] fileList=di.GetFiles;
ArrayList arr=new ArrayList();
foreach(FileInfo fi in fileList)
{
这里加个判断就可以了!
arr.Add(fi.FullName);
}
arrNames=(string[])arr.ToArray(typeof(string));
picMain.Image=Image.FromFile(arrNames[0]);
}
lextm 2008-01-22
  • 打赏
  • 举报
回复
使用OpenFileDialog会比用FolderBrowseDialog合适。那样可以用一楼的方法筛选了。
mohugomohu 2008-01-22
  • 打赏
  • 举报
回复
怎么筛呢?
changjiangzhibin 2008-01-22
  • 打赏
  • 举报
回复
筛选时就限定文件的后缀名

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧