简体   繁体   English

Directory.GetAllImageFiles我应该怎么做?

[英]Directory.GetAllImageFiles How should i do it?

I would like to get fill a String array with all the images found within a directory. 我想用在目录中找到的所有图像填充String数组。

Till now i use the following to get all the images with jpg format 直到现在我使用以下内容获取所有jpg格式的图像

Dim List() as string = Directory.GetFiles(Path, "*.jpg")

Now i would like to extend it and get all the image formats. 现在,我想扩展它并获取所有图像格式。

Could i use the directory.GetFiles combined with an " ImageFormat enumeration"? 我可以将directory.GetFiles与“ ImageFormat枚举”结合使用吗?

Hi you can use this which I found as community content at http://msdn.microsoft.com/en-us/library/wz42302f.aspx .: 嗨,您可以使用我在http://msdn.microsoft.com/zh-cn/library/wz42302f.aspx中作为社区内容找到的内容:

private static string[] GetFiles(string sourceFolder, string filters)
{
   return filters.Split('|').SelectMany(filter => System.IO.Directory.GetFiles(sourceFolder, filter)).ToArray();
}

an alternative which uses lazy evaluation (.Net 4.0 only): 使用延迟评估的替代方法(仅.Net 4.0):

private static IEnumerable<string> GetFiles(string sourceFolder, string filters)
{
   return filters.Split('|').SelectMany(filter => System.IO.Directory.EnumerateFiles(sourceFolder, filter));
}

You can use it like GetFiles("dir", "*.jpg|*.gif|*.jpeg|*.bmp|*.png") . 您可以像GetFiles("dir", "*.jpg|*.gif|*.jpeg|*.bmp|*.png") It is essentially just a search for each filter, so it is not as efficient as it can get. 从本质上来说,这只是对每个过滤器的搜索,因此效率尽如人意。

A final version is (is .Net 4.0 only, but can be made to a 2.0 solution at least): 最终版本是(仅.Net 4.0,但至少可以制成2.0解决方案):

private static IEnumerable<string> GetImageFiles(string sourceFolder)
{
   return from file in System.IO.Directory.EnumerateFiles(sourceFolder)
          let extension = Path.GetExtension(file)
          where extension == ".jpg" || extension == ".gif" || extension == ".png"
          select file;
}

I believe the last one is the fastest because it only loops once. 我相信最后一个是最快的,因为它只会循环一次。 But this depends on how the pattern search is implemented in Directory and how the OS searches. 但这取决于如何在Directory实施模式搜索以及OS如何进行搜索。 A performance test is needed which I haven't done. 我还没有进行性能测试。

This is 2.0 .net solution. 这是2.0 .net解决方案。

I did something similar in C#. 我在C#中做了类似的事情。 This solution used an folder as a drop off point for images to be processed. 该解决方案使用文件夹作为要处理图像的放置点。 Loading each type of file as an image is not correct solution in all cases but I wanted to validate each file as a loadable image. 在每种情况下将每种类型的文件加载为映像都不是正确的解决方案,但我想将每个文件验证为可加载的映像。

    string[] files = Directory.GetFiles(folderPath);
    foreach(string file in files)
    {


        System.Drawing.Image img = null;

        try
        {
            img = System.Drawing.Image.FromFile(file);
        }
        catch
        {
            // do nothing
        }

        if (img != null)
        {
            // did something

            img.Dispose();
        }
    }

Turns out I forgot a piece... Before I process the files, I did use a function to limit files being processed: 原来我忘了一件...在处理文件之前,我确实使用了一个函数来限制要处理的文件:

private bool IsImage(FileInfo file)
{
    bool imageFile = false;
    if ((file.Extension.ToLower() ==".jpg")||
       (file.Extension.ToLower() ==".gif")||
       (file.Extension.ToLower() == ".bmp") ||
       (file.Extension.ToLower() ==".png"))
    {
        imageFile = true;
    }

    return imageFile;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM