c# - Image.FromStream 内存不足异常

标签 c#

我正在尝试从 MemoryStream 获取图像。当在 MemoryStream 中我有一个大的 JPG 图像(在步骤中转换为位图后,大小从 38MB 增加到超过 1.3GB,评论为有问题)我得到一个 OutOfMemory 异常。使用较小的图像一切正常。如何处理这样的问题?对我来说可接受的解决方案是将存储在 _imgArray 中的图像调整大小/重新缩放到 1.3GB 以下。是否可以在调用 Image.FromStream 方法之前执行此操作?

public static Image GetImageFromByteArray(byte[] _imgArray)
{
    Image imgFromArray = null;
    MemoryStream stream = null;
    try
    {
        stream = new MemoryStream(_imgArray, 0, _imgArray.Length);
        imgFromArray = Image.FromStream(stream, true);//this line throws an Out of memory exception
    }
    catch(OutOfMemoryException)
    {
        Error.Warning("Das Bild ist zu groß!");
    }
    catch (Exception ex)
    {
        throw new FacadeException("Fehler beim Laden des Bildes.", ex);
    }
    finally
    {
        if (stream != null)
        {
            stream.Close();
        }
    }
    return imgFromArray;
}

最佳答案

原因:

当您在 32 位 (x86) 应用程序上创建大于 ~ 1.3GB 的对象时,将抛出 OutOfMemory 预期。

解决方案:

尝试将您的应用程序更改为 64 位(x64),如下图所示:

enter image description here

关于c# - Image.FromStream 内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44102724/

相关文章:

c# - VS 2008 中的混合环境调试 (C# Fortran)

c# - 为什么 HttpContext.Current.Profile 为 NULL?它如何获得值(value)?

c# - 如何使用表达式树调用带有引用变量的方法

c# - 具有指向指针参数的指针的 PInvoke 函数

c# - ImageProcessorCore : Opening image takes >11seconds, 我错过了什么?

c# - 在C#和Java中,Object[]和String[]之间的关系是什么?

c# - IIS MIME 类型的元数据库路径

C# MD5 哈希结果不是预期的结果

c# - 使用 C# 在富文本框中打开文件

c# - Visual Studio 搜索和替换行尾