WebImage and large memory consumption

450 views Asked by At

We have scenario of image upload in ASP.NET MVC3.

  • Controller

    public ActionResult Upload(IEnumerable<HttpPostedFileBase> images, SomeViewModel model)
    {
      foreach(var i in images)
      {
        ...
    
        byte[] fileBytes = i.InputStream.GetBytesArray();
    
        byte[] image = _imageManager.Resize(fileBytes, MaxImageWidth, MaxImageHeight, true); 
    
        ...
      }
    }
    
  • ImageManager

    public byte[] Resize(byte[] content, int width, int height, bool preserveAR = true)
    {
      if (content == null)
        return null;
    
      WebImage wi = new WebImage(content);
    
      wi = wi.Resize(width, height, preserveAspectRatio);
    
      return wi.GetBytes();
    }
    

So we recieve image from client as HttpPostedFileBase. We pass byte[] fileBytes to Resize method of imageManager. Image manager is creating new WebImage instance, then Resize image and transform it to byte[] again.

When debugging this code, at the moment I pass wi.GetBytes() line, my memory usage raises drastically (for at least 500mb). I`m uploading image of 10mb. When uploading smaller size photos (~1.5mb) memory consumption is normal.

What can be the cause of this, and can this be fixed somehow?

Thank you

1

There are 1 answers

0
Dean Ward On BEST ANSWER

Under the hood WebImage uses System.Drawing.Image.FromStream to take the original image stream and turn it into an array of bytes. I tried taking a 6.0MB JPG and calling that method on it and I got a stream with 6.0MB in it. If I ask for a BMP I get a 172MB byte aray

I suspect you're uploading a compressed image (e.g. PNG / JPG) and the call to GetBytes is causing the decompressed bytes of the image to be made available. When decompressed into it's raw form it could be that the image is actually substantially larger. Not much that you can do to get around this short of dealing with Stream objects the whole way so you never load everything into memory at once.