winform中获取指定文件夹下的所有图片

方法一:

C#的IO自带了一个方法
DirectoryInfo dir = new DirectoryInfo("文件夹名称");
dir.getFiles();//这个方法返回值就是Fileinfo类型的数组

再将获取的图片一一存入List数组中,需要从list中找即可

public String exePath = Application.StartupPath;

//绝对路径
DirectoryInfo dir = new DirectoryInfo("c:\\test");
//相对路径,和程序exe同目录下
//DirectoryInfo dir = new DirectoryInfo(@"test"); FileInfo[] fileInfo
= dir.GetFiles(); List<string> fileNames = new List<string>(); foreach (FileInfo item in fileInfo) { fileNames.Add(item.Name); }

//图片展示
 for (int i = 0; i < fileNames.Count; i++)
{
    string fileName = fileNames[i];
    this.panelAutographPic.Controls.Add(new PictureBox
    {
      BackColor = System.Drawing.Color.Transparent,
      BackgroundImageLayout = ImageLayout.Stretch,
      Width = 300,
      Height = 200,
    BackgroundImage = Image.FromFile(exePath + "../test/" + fileName)
    });
}

方法二:

将获取的图片一一存入ListBox中,需要从listBox中找即可

ListBox listBox1 = new ListBox();
private void Get_Folder(string FilePath)
{
  if (Directory.Exists(FilePath))
     {
           foreach (string d in Directory.GetFileSystemEntries(FilePath))
           {
              Image img = Image.FromFile(d);
                if (File.Exists(d) && img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg) || 
                        img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif) || 
                        img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp) || 
                        img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
                    {
                        listBox1.Items.Add(d.ToString());
                    }
            }
       }
       else
       {
            MessageBox.Show("文件夹不存在!");
       }
}
//调用
Get_Folder(@"指定文件夹名");

 

转载于:https://www.cnblogs.com/qiantao/p/9832021.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值