Directory.GetAllImageFiles 我应该怎么做?

发布于 2024-10-16 20:52:12 字数 243 浏览 4 评论 0原文

我想用目录中找到的所有图像填充一个字符串数组。

到目前为止,我使用以下方法获取所有 jpg 格式的图像

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

现在我想扩展它并获取所有图像格式。

我可以将directory.GetFiles 与“ImageFormat 枚举”结合使用吗?

I would like to get fill a String array with all the images found within a directory.

Till now i use the following to get all the images with jpg format

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"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

公布 2024-10-23 20:52:12

您好,您可以使用我在 http://msdn.microsoft 找到的社区内容.com/en-us/library/wz42302f.aspx.:

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

使用延迟计算的替代方案(仅限.Net 4.0):

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

您可以像 GetFiles("dir", "*.jpg| *.gif|*.jpeg|*.bmp|*.png")。它本质上只是对每个过滤器的搜索,因此它没有尽可能高效。

最终版本是(仅限.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;
}

我相信最后一个版本是最快的,因为它只循环一次。但这取决于Directory中模式搜索的实现方式以及操作系统的搜索方式。需要进行性能测试,但我还没有做过。

Hi you can use this which I found as community content at http://msdn.microsoft.com/en-us/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):

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"). 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):

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. A performance test is needed which I haven't done.

晨光如昨 2024-10-23 20:52:12

这是 2.0 .net 解决方案。

我在 C# 中做了类似的事情。该解决方案使用文件夹作为要处理的图像的下落点。在所有情况下,将每种类型的文件作为图像加载并不是正确的解决方案,但我想将每个文件验证为可加载图像。

    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();
        }
    }

原来我忘记了一块...
在处理文件之前,我确实使用了一个函数来限制正在处理的文件:

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;
}

This is 2.0 .net solution.

I did something similar in 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文