需求:一个搜索的WINFORM程序 。输入一个图片的尺寸范围、 图片格式 、指定一个要搜索的文件夹把搜到的所有图片路径以列表的形式陈列出来。(路径让人选择就好了,此处有待改进)浏览文件,winform的用OpenFileDialog ;webform的用fileupload
显示图片路径列表用listview,设置view属性:Details;Columns属性:路径。界面效果如下:
本功能涉及shell.dll,第一步:添加引用,从COM中引用 Microsoft Shell Controls and Automation在.cs文件中添加命名空间using Shell32。
private void btnFind_Click(object sender, EventArgs e)
{
int imageWidth = Convert.ToInt32(txtWidth.Text.Trim());
int imageWidth2 = Convert.ToInt32(txtWidth2.Text.Trim());
int imageHeigth = Convert.ToInt32(txtHeigth.Text.Trim());
int imageHeigth2 = Convert.ToInt32(txtHeigth2.Text.Trim());
string imageType = "*." + txtImageType.Text.Trim();//这里无论大小写都查出来了,所以不用转换“gif、GIF”
string path = txtPath.Text.Trim();
string[] files = Directory.GetFiles(@path, imageType);//返回指定路径下用某个关键字搜索得到的结果
foreach (string filePath in files)
{
string sFile = filePath;//单张图片判断是否符合条件
// Creating a ShellClass Object from the Shell32
ShellClass sh = new ShellClass();
// Creating a Folder Object from Folder that inculdes the File
Folder dir = sh.NameSpace(Path.GetDirectoryName(sFile));//文件目录路径,即文件夹路径
// Creating a new FolderItem from Folder that includes the File
FolderItem item = dir.ParseName(Path.GetFileName(sFile));//文件路径
string strwidth = dir.GetDetailsOf(item, 27);//水平像素 "1204像素"
string strheigth = dir.GetDetailsOf(item, 28);//垂直像素 "1204像素"
int width = Convert.ToInt32(strwidth.Substring(0,strwidth.Length-2));//"1024"去掉“像素”这2个字
int heigth = Convert.ToInt32(strheigth.Substring(0, strheigth.Length - 2));
if (width >= imageWidth && width <= imageWidth2 && heigth >= imageHeigth && heigth <= imageHeigth2)
{
/*
ListViewItem item = new ListViewItem();
item.SubItems[0].Text = "第一列";
item.SubItems.Add("第二");
item.SubItems.add("第三");
*/
ListViewItem li = new ListViewItem();
li.SubItems.Clear();//每次填充前要将上一次的清空
li.SubItems[0].Text = filePath;
// Create a helper Object for holding the current Information
// an put it into a ArrayList
lvShowImagePath.Items.Add(li);//用listview加载数据
}
}
{
int imageWidth = Convert.ToInt32(txtWidth.Text.Trim());
int imageWidth2 = Convert.ToInt32(txtWidth2.Text.Trim());
int imageHeigth = Convert.ToInt32(txtHeigth.Text.Trim());
int imageHeigth2 = Convert.ToInt32(txtHeigth2.Text.Trim());
string imageType = "*." + txtImageType.Text.Trim();//这里无论大小写都查出来了,所以不用转换“gif、GIF”
string path = txtPath.Text.Trim();
string[] files = Directory.GetFiles(@path, imageType);//返回指定路径下用某个关键字搜索得到的结果
foreach (string filePath in files)
{
string sFile = filePath;//单张图片判断是否符合条件
// Creating a ShellClass Object from the Shell32
ShellClass sh = new ShellClass();
// Creating a Folder Object from Folder that inculdes the File
Folder dir = sh.NameSpace(Path.GetDirectoryName(sFile));//文件目录路径,即文件夹路径
// Creating a new FolderItem from Folder that includes the File
FolderItem item = dir.ParseName(Path.GetFileName(sFile));//文件路径
string strwidth = dir.GetDetailsOf(item, 27);//水平像素 "1204像素"
string strheigth = dir.GetDetailsOf(item, 28);//垂直像素 "1204像素"
int width = Convert.ToInt32(strwidth.Substring(0,strwidth.Length-2));//"1024"去掉“像素”这2个字
int heigth = Convert.ToInt32(strheigth.Substring(0, strheigth.Length - 2));
if (width >= imageWidth && width <= imageWidth2 && heigth >= imageHeigth && heigth <= imageHeigth2)
{
/*
ListViewItem item = new ListViewItem();
item.SubItems[0].Text = "第一列";
item.SubItems.Add("第二");
item.SubItems.add("第三");
*/
ListViewItem li = new ListViewItem();
li.SubItems.Clear();//每次填充前要将上一次的清空
li.SubItems[0].Text = filePath;
// Create a helper Object for holding the current Information
// an put it into a ArrayList
lvShowImagePath.Items.Add(li);//用listview加载数据
}
}
}