是否有一个原因Image.FromFile抛出了一个无效的图像格式一个OutOfMemoryException? [英] Is there a reason Image.FromFile throws an OutOfMemoryException for an invalid image format?

查看:336
本文介绍了是否有一个原因Image.FromFile抛出了一个无效的图像格式一个OutOfMemoryException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写code,捕捉这个 OutOfMemoryException异常并抛出一个新的,更直观的异常:

I am writing code that catches this OutOfMemoryException and throws a new, more intuitive exception:

/// ...
/// <exception cref="FormatException">The file does not have a valid image format.</exception>
public static Image OpenImage( string filename )
{
    try
    {
        return Image.FromFile( filename );
    }
    catch( OutOfMemoryException ex )
    {
        throw new FormatException( "The file does not have a valid image format.", ex );
    }
}

这是code接受其用户,或者是 OutOfMemoryException异常有意抛出一个特别的原因?

Is this code acceptable to its user, or is OutOfMemoryException intentionally being thrown for a particular reason?

推荐答案

没有,这就是历史。 GDI +是写好半天才.NET曾经苏醒过来。它的SDK包装是用C ++编写。例外的是在C ++中玄乎,不是每个人都买放进去。谷歌不会为例子。因此,要保持它兼容它报告错误codeS问题。这只是从来没有很好地进行扩展,程序员库使它成为一个目标,有意限制可能出现的错误codeS的数目,减少对向公司报告它们的客户端程序员的负担。

No, it is history. GDI+ was written quite a while before .NET ever came around. The SDK wrapper for it was written in C++. Exceptions are iffy in C++, not everybody buys into them. Google doesn't for example. So to keep it compatible it reports problems with error codes. That just never scales well, library programmers make it a goal to intentionally limit the number of possible error codes, it lessen the burden on the client programmer having to report them.

GDI +在黑桃这个问题,只定义了20错误codeS。那是的的多了一大块空间code的有这么多的外部依赖性。这本身就是一个问题,有一个极大的方式把事情弄糟的图像文件。没办法,一个库的错误报告可以足够精细的覆盖他们。这些错误codeS被采摘长.NET之前定义的标准异常派生类型肯定是没有帮助的事实。

GDI+ has this problem in spades, it defines only 20 error codes. That is not much for such a large chunk of code with so many external dependencies. Which in itself is a problem, there are a gazillion ways to mess up an image file. No way that a library's error reporting can be fine-grained enough to cover them all. The fact that these error codes were picked long before .NET defined standard Exception derived types certainly didn't help.

状态:: OutOfMemory错误code得到了重载意味着不同的事情。有时它确实意味着内存不足,无法分配足够的空间来存储位图位。可悲的是,图像文件格式问题是由同样的错误code的报道。这里的摩擦是,它不可能决定是否宽*高*像素它从图像文件中读取是一个问题,因为没有足够的可用于位图存储。或者,如果在图像文件中的数据是垃圾。它假定图像文件是不是垃圾,公平的电话,那是另外一个程序的问题。所以OOM是它的报告。

The Status::OutOfMemory error code got overloaded to mean different things. Sometimes it really does mean out of memory, it can't allocate enough space to store the bitmap bits. Sadly, an image file format problem is reported by the same error code. The friction here is that it cannot possibly decide if the width * height * pixels it read from the image file is a problem because there isn't enough storage available for the bitmap. Or if the data in the image file is junk. It assumes that image file is not junk, fair call, that's another program's problem. So OOM is what it reports.

有关完整性,这些都是错误codeS:

For completeness, these are the error codes:

enum Status
{
    Ok = 0,
    GenericError = 1,
    InvalidParameter = 2,
    OutOfMemory = 3,
    ObjectBusy = 4,
    InsufficientBuffer = 5,
    NotImplemented = 6,
    Win32Error = 7,
    WrongState = 8,
    Aborted = 9,
    FileNotFound = 10,
    ValueOverflow = 11,
    AccessDenied = 12,
    UnknownImageFormat = 13,
    FontFamilyNotFound = 14,
    FontStyleNotFound = 15,
    NotTrueTypeFont = 16,
    UnsupportedGdiplusVersion = 17,
    GdiplusNotInitialized = 18,
    PropertyNotFound = 19,
    PropertyNotSupported = 20,
#if (GDIPVER >= 0x0110)
    ProfileNotFound = 21,
#endif //(GDIPVER >= 0x0110)
};

这篇关于是否有一个原因Image.FromFile抛出了一个无效的图像格式一个OutOfMemoryException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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