Search code examples
c#asp.net-mvcimagejquery-file-uploadhttppostedfilebase

HttpPostedFileBase to Image throws exceptions


I am uploading a big image ~10Mb and I have the following code:

public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
    foreach (var file in files)
    {
        var image = Image.FromStream(file.InputStream, true, true);
        ...
    }
}

Sometime it throws Out of Memory, sometimes GDI+ generic errors. I cannot reproduce this in a console app with the following code:

using (FileStream stream = File.Open(@"d:\test.jpg", FileMode.Open))
{
    var image = Image.FromStream(stream);
}

What can be the cause for those exceptions? One note: for small images everything works great.


Solution

  • I have read answers on many similar exceptions and none could be applied to my case. Image itself was not corrupted or with incorrect bits. The thing was that I was debugging in 32 bit mode. As soon as I change this setting in VS2015 it worked like a charm. Seems Image.FromStream requires a lot of RAM for big images.

    Tools -> Options -> Projects and Solutions -> Web Projects -> Use the 64 bit...