- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在编写代码来捕获此 OutOfMemoryException
并抛出一个新的、更直观的异常:
/// ...
/// <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 );
}
}
此代码是否为用户所接受,或者 OutOfMemoryException
是否出于特定原因有意抛出?
最佳答案
不,这是历史。 GDI+ 是在 .NET 出现之前编写的。它的 SDK 包装器是用 C++ 编写的。异常在 C++ 中是不确定的,并不是每个人都接受它们。例如,谷歌没有。因此,为了保持兼容性,它会报告错误代码的问题。这永远无法很好地扩展,库程序员将有意限制可能的错误代码的数量作为目标,它减轻了客户端程序员必须报告它们的负担。
GDI+ 确实存在这个问题,它只定义了 20 个错误代码。对于具有如此多外部依赖项的如此大的代码块来说,这不算太多。这本身就是一个问题,有无数种方法可以弄乱图像文件。库的错误报告不可能足够细化以涵盖所有错误。这些错误代码早在 .NET 定义标准异常派生类型之前就已被挑选出来,这一事实当然无济于事。
Status::OutOfMemory 错误代码重载意味着不同的事情。有时它确实意味着内存不足,它无法分配足够的空间来存储位图位。遗憾的是,图像文件格式问题由相同的错误代码报告。这里的摩擦是它无法确定它从图像文件中读取的宽度 * 高度 * 像素是否有问题,因为没有足够的存储空间可用于位图。或者如果图像文件中的数据是垃圾。它假设图像文件不是垃圾,公平的调用,那是另一个程序的问题。所以 OOM 就是它报告的内容。
为了完整起见,这些是错误代码:
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)
};
关于c# - Image.FromFile 为无效图像格式抛出 OutOfMemoryException 是否有原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2610416/
我已经花了几周的时间来解决这个问题。但我仍然无法解决这个问题。 我在 angularjs 中使用 http 调用 Web API 服务 $http({
我的转储文件包含 OutOfMemoryException 但所有对象字段似乎都为空。这只是一个误报还是有办法知道这个异常(exception)? 0:052> !do 000000027fff10e
我使用的项目包含许多从单个基类继承的类。在单元测试中,我需要按类型和数据比较收到的结果。 当我在条件列表包含足够多的不同条件的情况下使用类型匹配比较时,编译器会抛出 OutOfMemoryExcept
我的应用程序在 512mb 手机模拟器上运行时突然崩溃(可重现)(只有 920 来测试)。没有任何异常,它只是变黑了。然后我在应用程序上运行分析工具,发现内存在应用程序崩溃时达到了极限。应用程序退出并
我试图弄清楚应该如何使用 MemoryCache 以避免出现内存不足异常。我来自 ASP.Net 背景,缓存管理它自己的内存使用,所以我希望 MemoryCache 会做同样的事情。正如我制作的波纹管
我从某个生产代码版本中获得了异常日志。 System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was
比如说,我们有一个表,其中包含一些包含 jpg 文件二进制数据的大型 text 字段。任务是从磁盘上的数据库中获取这些文件。因此,起初我决定执行以下操作: MyDataContext dc = new
在 Windows 窗体 C# 应用程序中,我有一个文本框,用户可以在其中粘贴日志数据,并对其进行排序。我需要单独检查每一行,所以我按新行拆分输入,但如果有很多行,超过 100,000 行左右,它会抛
我们正在将一个大的平面文件加载到 BizTalk Server 2006(原始版本,不是 R2)中 - 大约 125 MB。我们针对它运行一个映射,然后获取每一行并调用一个存储过程。 我们在编排处理期
比如说,我们有一个表,其中包含一些包含 jpg 文件二进制数据的大型 text 字段。任务是从磁盘上的数据库中获取这些文件。因此,起初我决定执行以下操作: MyDataContext dc = new
在我的应用程序中,用户可以启动相机 Intent 并拍照并将其保存到自定义文件夹中。然后他们可以重新启动相机拍摄另一张照片等。因此最终用户可以拍摄多张照片。 我遇到的问题是,在我的画廊中,当我尝试将所
在 Windows 窗体 C# 应用程序中,我有一个文本框,用户可以在其中粘贴日志数据,并对其进行排序。我需要单独检查每一行,所以我按新行拆分输入,但如果有很多行,超过 100,000 行左右,它会抛
我收到 OutOfMemoryException,而堆似乎仍然有足够的剩余空间。 我自己的日志报告 - VM 内存使用了 48G 中的 22.28G 来自-XX:+PrintGCDetails -XX
给定一个简单的案例类 case class Rating(user: Int, item: Int, rating: Double) 和两个RDD[Rating]:我们得到的 OOME 在两个 R
我正在开发一个应用程序,该应用程序使用移动设备拍照并使用网络服务发送照片。但是在我拍了 4 张照片之后,我在下面的代码中得到了一个 OutOfMemoryException。我尝试调用 GC.Coll
我想调整我网站中图片的大小,但是当我使用 Bitmap 加载 14032*19864(png 扩展名)的图片时,抛出 OutOfMemoryException。我的编译器配置是 any cpu。我在怀
在我的应用程序中,我正在使用 Windows 服务读取文件。该服务每秒读取一次文件。这是我用来读取文件的代码: public static byte[] GetBytesFromFile(string
我正在尝试将用户的整个桌面捕获为图像。我通过以下方式执行此操作: public Bitmap CaptureScreen() { // Set up a bit
我正在开发一个 64 位 .Net Windows 服务应用程序,该应用程序本质上加载了一堆数据以供处理。在执行数据量测试时,我们能够压倒该进程并抛出 OutOfMemoryException(我没有
我知道我的问题没有简单的答案,但我会很感激想法、指南或某种要看的东西列表 我有一个不断抛出 OutOfMemoryException 异常的网络 Windows 服务。该服务有两个针对 x86 和 x
我是一名优秀的程序员,十分优秀!