如何确定文件是否在.NET中的图像文件? [英] How can I determine if a file is an image file in .NET?

查看:120
本文介绍了如何确定文件是否在.NET中的图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不希望依赖于文件扩展名。我不在乎知道什么类型的图像是(.JPG,.PNG等),我只是想知道,如果该文件是一个图像或没有。我想preFER如果可能,不使用任何non-.NET的DLL。

我知道如何做到这一点的最好办法是:

 布尔isImageFile;
尝试
{
    Image.FromFile(镜像文件).Dispose();
    isImageFile = TRUE;
}
赶上(OutOfMemoryException异常)
{
    isImageFile = FALSE;
}
 

由于这里要注意: http://msdn.microsoft.com/en-美国/库/ stf701f5.aspx Image.FromFile()抛出 OutOfMemoryException异常,如果该文件ISN 'T一个有效的图像格式。使用上面给我的正是我想要的结果,不过我倒是preFER不使用它的原因如下:

  • 这是我的信念,即使用try-接球程序的正常执行是出于性能方面的一个不好的做法。
  • Image.FromFile()加载整个图像文件(如果它是一个图像文件)到内存中。这是一种浪费,我想是因为我只需要的文件类型,并不需要做任何进一步的图像处理,在这一点上我的code。
  • 我不喜欢追赶 OutOfMemoryException异常是因为,如果有一个真正的东西了内存不足的问题,我的程序吞下它,并不断走向何方?

有没有更好的办法来做到这一点?任何/所有我上面列出的无端忧虑?

编辑:由于这里接受的答案,这些都是三解我现在所知道的:

  1. 通过加载在内存中的整个图像Image.FromFile()和一个try-catch。
    • 赞成的:是否对图像文件内容的更深层次的检查;包括许多图像类型。
    • 缺点的:最慢的;从try-catch代码和加载完整映像文件到内存开销;从追赶一个真正的OutOfMemoryException异常的潜在危险。
  2. 检查头字节的图像文件。
    • 赞成的:快速,低内存占用
    • 缺点的:有可能变脆;需要为每个文件类型的程序。
  3. 检查文件扩展名。
    • 赞成的:最快;最简单的。
    • 缺点的:不在所有情况下正常工作;最容易错的。

(我没有看到一个明确的赢家,因为我可以想像的情况是,每个人是合适的。对于我的应用程序的目的,文件类型检查发生频繁,以至于方法1的性能问题不是一个的问题。)

解决方案
  1. 您将只注意到从异常的性能损失,如果你经常把他们。所以,除非你的程序期望看到许多无效的影像(每秒数百个),你不应该注意到异常处理的开销。
  2. 这是真的分辨图像是一个完整的图像或损坏的唯一途径。您可以检查标题为其他人的建议,但只检查,看看是否开始几个字节是正确的,什么都可能是垃圾。这是否是足够好与否取决于应用程序的需求。光看标题可能对你的使用情况不够好。
  3. 是的,这是对BCL团队的部分设计比较差。如果要加载很多大的图像,你很可能会击中在大对象堆一个真正的OOM的情况。据我所知,目前还没有办法区分这两个例外。

I don't want to rely on the file extension. I don't care to know what type of image it is (.jpg, .png, etc.), I simply want to know if the file is an image or not. I'd prefer to not use any non-.NET dlls if possible.

The best way I know how to do this is the following:

bool isImageFile;
try
{
    Image.FromFile(imageFile).Dispose();
    isImageFile = true;
}
catch (OutOfMemoryException)
{
    isImageFile = false;
}

As noted here: http://msdn.microsoft.com/en-us/library/stf701f5.aspx, Image.FromFile() throws an OutOfMemoryException if the file isn't a valid image format. Using the above gives me exactly the result I want, however I'd prefer not to use it for the following reasons:

  • It is my belief that using try-catches for normal program execution is a bad practice for performance reasons.
  • Image.FromFile() loads the whole image file (if it is an image file) into memory. This is wasteful I assume because I only need the file type and don't need to do any further image manipulation at this point in my code.
  • I dislike catching OutOfMemoryExceptions because what if there is a REAL out-of-memory problem and my program swallows it and keeps going?

Are there any better ways to do this? Or, are any/all of my concerns listed above unwarranted?

Edit: Since receiving the answers here, these are the three solutions I'm now aware of:

  1. Load the whole image in memory via Image.FromFile() and a try-catch.
    • Pros: Does a deeper check against the image files contents; covers many image types.
    • Cons: Slowest; overhead from try-catch and loading full image file into memory; potential danger from catching a 'real' OutOfMemoryException.
  2. Check the header bytes of the image file.
    • Pros: Quick, low memory usage.
    • Cons: potentially brittle; need to program for every file type.
  3. Check the file extension.
    • Pros: Quickest; simplest.
    • Cons: Doesn't work in all situations; most easily wrong.

(I do not see a clear "winner" since I can imagine a situation in which each one would be appropriate. For my application's purposes, the file type checking happens infrequently enough that the performance concerns of method 1 weren't an issue.)

解决方案

  1. You will only notice a performance hit from exceptions if you are constantly throwing them. So unless your program expects to see many invalid images (hundreds per second) you should not notice the overhead of exception handling.
  2. This is really the only way to tell if the image is a full image or corrupt. You can check the headers as the other people recommend, but that only checks to see if the beginning few bytes are correct, anything else could be garbage. Whether this is good enough or not depends on the requirements of your application. Just reading the header might be good enough for your use case.
  3. Yes, this is rather poor design on the BCL team's part. If you are loading many large images you very well could be hitting a real OOM situation in the large object heap. As far as I know, there is no way to differentiate the two exceptions.

这篇关于如何确定文件是否在.NET中的图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆