Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebImage and large memory consumption

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

like image 788
22db05 Avatar asked Jun 11 '26 23:06

22db05


1 Answers

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.

like image 50
Dean Ward Avatar answered Jun 14 '26 08:06

Dean Ward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!