0

I get these errors every once in a while and I am not sure why. This code executes thousands of times a day and Ill get these errors every once in a while. One of the images is 94.9 KB, 1024x1024 image. The image is being read from an Azure File Storage disk via UNC Path.

System.OutOfMemoryException: Out of memory.

Generated: Sat, 23 Apr 2016 15:09:54 GMT

System.OutOfMemoryException: Out of memory.
   at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
   at System.Drawing.Image.FromFile(String filename)
   at Tournaments.ImageHandler.ProcessRequest(HttpContext context) in C:\Development\Exposure\Main\Websites\Tournaments\ImageHandler.ashx.cs:line 64
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Actual Code

 using (var image = Image.FromFile(path))
 {
 }
7
  • It suggests there is a memory leak somewhere. Commented Apr 23, 2016 at 15:16
  • I posted the code above. Memory leak within the .NET framework? Commented Apr 23, 2016 at 15:20
  • 1
    The memory leak is likely in Tournaments.ImageHandler.ProcessRequest, however, this can also be caused by a image that is just too large, or that has bad header data. Commented Apr 23, 2016 at 15:23
  • Well I opened the same URL in the browser and loaded fine. It is a big image though but why would this cause this? The image really isnt that large as I posted a size of one above. Commented Apr 23, 2016 at 15:25
  • 2
    Note that GDI+ is really bad at identifying the reason for the OutOfMemoryException. It could be that the Image Header is currupted, or a number of different reasons! Check out the answer on this question! Commented Apr 23, 2016 at 15:58

1 Answer 1

2

This seemed to fix my issue as it doesn't hold a reference to it this way.

using (var memoryStream = new MemoryStream(File.ReadAllBytes(path)))
            {
                using (var image = Image.FromStream(memoryStream))
                {
                  
                    byte[] bytes;

                
                      
                        using (var memoryStream1 = new MemoryStream())
                        {
                            image.Save(memoryStream1, GetImageFormat(Path.GetExtension(path)));
                            bytes = memoryStream1.ToArray();
                        }
                    

                }
            }
    }
    
      private ImageFormat GetImageFormat(string extension)
            {
                switch (extension.ToLower())
                {
                    case ".png":
                        return ImageFormat.Png;
                    default:
                        return ImageFormat.Jpeg;
                }
            }
Sign up to request clarification or add additional context in comments.

2 Comments

This fix did not work for me. I have the same access denied or Out of Memory error as if I used using (Image MyImage = System.Drawing.Image.FromFile(FileName)) or using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Looks like I am using different code now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.