System.Drawing.Image.FromFile" System.OutOfMemoryException:内存不足。"

时间:2016-04-23 15:12:11

标签: c# .net out-of-memory system.drawing

我偶尔会遇到这些错误,我不知道为什么。此代码每天执行数千次,并且每隔一段时间就会出现这些错误。其中一个图像是94.9 KB,1024x1024图像。正在通过Azure File Storage路径从UNC磁盘读取图像。

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)

实际代码

 using (var image = Image.FromFile(path))
 {
 }

1 个答案:

答案 0 :(得分:1)

这似乎解决了我的问题,因为它没有以这种方式引用它。

  using (var memoryStream = new MemoryStream(File.ReadAllBytes(path)))
            {
                using (var image = Image.FromStream(memoryStream))
                {