简体   繁体   English

Image.FromStream OutOfMemory异常

[英]Image.FromStream OutOfMemory Exception

I'm trying to get image from MemoryStream. 我正在尝试从MemoryStream获取图像。 When in MemoryStream I have a big JPG image (which size increases from 38MB to over 1.3GB after converting to bitmap in step commented as problematic) I get an OutOfMemory exception. 在MemoryStream中,当我有一个很大的JPG图像(在步骤中注释为有问题的步骤转换为位图后,其大小从38MB增加到1.3GB以上),我收到了OutOfMemory异常。 With smaller images everything works fine. 使用较小的图像,一切正常。 How to handle such problem? 如何处理这样的问题? Acceptable solution for me is to resize/rescale image stored in _imgArray below 1.3GB. 对我来说,可接受的解决方案是将存储在_imgArray中的图像调整为1.3GB以下。 Is it possible to do this before invoking Image.FromStream method? 可以在调用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;
}

Reason: 原因:

The OutOfMemory expection will be throwed when you create an object that is greater than ~ 1.3GB on an application 32bit(x86). 当您在应用程序32位(x86)上创建大于〜1.3GB的对象时,将抛出OutOfMemory期望。

Solution: 解:

Try to change your applicationo to 64bit(x64) as the picture below: 尝试将应用程序更改为64bit(x64),如下图所示:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM