Start from memory resizing of the image in the grid of points

I am having a lot of problems causing images to resize. At first I was sure that I was just leaking, so I switched my own code to something based on https://github.com/JimBobSquarePants/ImageProcessor and the same problems occur.

So the original image causing the problem might be 7137x10096 Uncompressed, which should be 7137x10096x4 = 288220608 bytes or 274 MB. Okay, this is "big", but why is this a concern? Running it, IISExpress (32 bit) throws it out of memory. Running on Azure on a 64-bit server with 3.5 GB is simply unreliable.

Error: "An exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll but was not handled in user code"

ImageProcessor based code issue imageFactory.Load command

   public bool ProcessStream(Size size, Func<MemoryStream, bool> resultCallback, string gravity, Stream inStream, int quality=80)
    {
        inStream.Position = 0;
        bool res = false;
        using (var outStream = new MemoryStream())
        {
            using (var imageFactory = new ImageFactory())
            {
                var anchor = AnchorPosition.Center;
                if (gravity == "top") anchor = AnchorPosition.Top;
                if (gravity == "bottom") anchor = AnchorPosition.Bottom;
                var layer = new ResizeLayer(size, ResizeMode.Pad, anchor);

                imageFactory.Load(inStream)
                    .Resize(layer)
                    .BackgroundColor(System.Drawing.ColorTranslator.FromHtml("#FFFFFF"))
                    .Quality(quality)
                    .Save(outStream);
                res = resultCallback(outStream);
            }
        }
        return res;
    }

      

In my simplest test case, I call this with

     public bool nop(MemoryStream s)
     {
         return true;
     }

     public string TestResize()
     {
         var resizer = new ResizeImage();
         var size = new System.Drawing.Size(300, 400);
         using (
             var filestream =
                 new FileStream(
                     @"C:\Users\Andrew\problem issues\NastyLargeImageThatBreaksThings.jpg",
                     FileMode.Open, FileAccess.Read))
         {
             var res = resizer.ProcessStream(size, nop, "top", filestream, 75);
             return res.ToString();
         }
     }

      

I can:

1) Try another method

2) Upgrade my Azure servers to have more memory (not sure if this helps)

3) Try resizing with a third party service like http://www.blitline.com/ that has some Azure integration.

Any insight as to why images of this size cannot be processed in Systen.Drawing would be much appreciated.

+3


source to share





All Articles