System.Drawing.Image.FromFile() 上的内存不足异常
我有一个创建缩略图的图像上传器和裁剪器,我偶尔会在以下行中遇到内存不足异常:
Dim bm As Bitmap = System.Drawing.Image.FromFile(imageFile)
错误的发生很小而且非常罕见,但我总是想知道可能导致它的原因。 imageFile 变量只是图像路径的 Server.MapPath。
我很好奇是否有人以前经历过这个问题,以及他们是否有任何想法可能会导致这个问题? 也许是图像的大小?
如果有必要,我可以发布代码以及我拥有的任何支持信息,但很想听听人们对此的意见。
I have an image uploader and cropper which creates thumbnails and I occasionally get an Out Of Memory exception on the following line:
Dim bm As Bitmap = System.Drawing.Image.FromFile(imageFile)
The occurance of the error is tiny and very rare, but I always like to know what might be causing it. The imageFile variable is just a Server.MapPath to the path of the image.
I was curious if anyone had experience this issue previously and if they had any ideas what might be causing it? Is it the size of the image perhaps?
I can post the code if necessary and any supporting information I have, but would love to hear people's opinions on this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
值得一提的是,OutOfMemoryException 并不总是真正意味着内存不足 - 特别是在处理文件时。 我相信如果您由于某种原因用完手柄,也可能会发生这种情况。
在使用完所有位图后,您是否要处理掉它们? 对于单个图像,这种情况会重复发生吗?
It's worth knowing that OutOfMemoryException doesn't always really mean it's out of memory - particularly not when dealing with files. I believe it can also happen if you run out of handles for some reason.
Are you disposing of all your bitmaps after you're done with them? Does this happen repeatably for a single image?
如果这不是一个坏图像文件,但实际上是
Image.FromFile
的正常问题,其中它使文件句柄保持打开状态,那么解决方案是使用Image.FromStream
代替。使用显式的
Dispose()
、using()
语句或在位图上将值设置为null
并不能解决 <代码>Image.FromFile。因此,如果您的应用程序运行一段时间并打开很多文件,请考虑使用 Image.FromStream() 代替。
If this wasn't a bad image file but was in fact the normal issue with
Image.FromFile
wherein it leaves file handles open, then the solution is useImage.FromStream
instead.Using an explicit
Dispose()
, ausing()
statement or setting the value tonull
on the bitmap doesn't solve the issue withImage.FromFile
.So if you App runs for a time and opens a lot of files consider using
Image.FromStream()
instead.今天,我在为充满图像的文件夹创建缩略图时遇到了同样的问题。 事实证明,“内存不足”每次都在同一时间点发生。 当我查看包含要转换的图像的文件夹时,我发现造成问题的文件是thumbs.db。 我添加了一些代码以确保仅转换图像文件并解决问题。
我的代码基本上是
希望这有帮助。
I hit the same issue today while creating Thumbnail images for a folder full of images. It turns out that the "Out Of Memory" occured exactly at the same point each time. When I looked at the folder with the images to be converted I found that the file that was creating the problem was thumbs.db. I added some code to make sure that only image files were being converted and the issue was resolved.
My code is basically
Hope this helps.
另请检查您是否没有在其他地方打开相同的文件。 显然,当你打开一个文件两次时(即使使用 File.Open())也会抛出 OutOfMemoryException...
Also check if you haven't opened the same file somewhere else. Apparently, when you open a file twice (even with File.Open()) OutOfMemoryException is thrown too...
您也可以以阅读模式打开它,(如果您想同时在两个地方使用它)
Also you can open it in read mode, (if you want to use it in two place same time)
当图像文件损坏时会发生这种情况。 这是一个糟糕的错误消息,因为内存与它无关。 我还没有编写出代码,但是 try/catch/finally 将阻止程序异常终止。
This happens when the image file is corrupted. It is a bad error message, because memory has nothing to do with it. I haven;t worked out the coding, but a try/catch/finally will stop the program from abending.
今天,当我尝试调整图像大小然后裁剪它时,我遇到了类似的问题,发生的事情是我使用此代码来调整图像大小。
然后是裁剪代码...
然后这就是我调用上面代码的方式...
我在裁剪部分不断收到错误,缩放器工作正常!
事实证明,缩放器内部发生的事情在裁剪函数中引发了错误。 调整大小的计算使图像的实际尺寸变得像 399 而不是我传入的 400。
因此,当我传入 400 作为裁剪参数时,它试图用400px 宽度的 bmp 并抛出内存不足错误!
上述大部分代码可以在 http:// /www.switchonthecode.com/tutorials/csharp-tutorial-image-editing- saving-cropping-and-resizing
I had a similar problem today when I was trying to resize an image and then crop it, what happened is I used this code to resize the image.
And then this code for the crop...
Then this is how I called the above code...
I kept getting errors on the crop part, the resizer worked fine!
Turns out, what was happening inside the resizer was throwing errors in the crop function. The resized calculations were making the actual dimensions of the image come out to be like 399 rather than 400 that I passed in.
So, when I passed in 400 as the argument for the crop, it was trying to crop a 399px wide image with a 400px width bmp and it threw the out of memory error!
Most of the above code was found on http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing
如果图像是图标,则需要不同的加载处理,例如下一个函数:
If an image is an icon then different loading handling is required, like in next function:
我编写的用于将 TIFF 转换为 PDF 的实用程序也遇到了同样的问题。 我经常会在与您相同的行上遇到“内存不足”错误。
然后我发现只有当文件扩展名是“.tiff”时才会发生错误,并且在我用扩展名“.tif”重命名它后工作正常
I had the same problem with a utility I wrote to convert TIFF(s) to PDF(s). Often I would get the "out of memory" error on the same line as you.
Then I discovered the error only happened when the file extension was ".tiff" and worked fine after I renamed it with an extension of ".tif"
我遇到了同样的问题,在查看代码中的其他位置之前,我想确保是否可以使用任何图像查看器打开图像,并发现图像已损坏/损坏,尽管它是一个 1KB 大小的 .PNG 文件。 在同一位置添加了一个新图像,然后它工作得很好。
I have had the same issue, before looking else where in the code wanted to make sure if I can open the Image with any Image viewer and figured out that the Image is corrupted/damaged though it's a .PNG file with 1KB size. Added a new Image in the same location, then It worked fine.
我在批处理 Tiff 文件时遇到同样的问题。 大多数文件不会引发异常,但很少有文件在 ASP.NET 4.0 中引发“内存不足”异常。 我已经使用二进制数据来找出为什么只针对少数文件并且来自同一文件夹。 这不可能是 ASP.NET ASPNET 或 NETWORK SERVICE 帐户的权限问题,因为其他文件是工作文件。
我打开iTextSharp.text.Image类,发现有很多GetInstance()的重载方法。 我已经使用以下代码解决了我的问题: 注意:catch 块将为有问题的文件运行。
I am having same problem batch processing Tiff files. Most of the files aren't throwing an exception but few files are throwing "Out of Memory" exception in ASP.NET 4.0. I have used binary data to find out why just for few files and from within same folder. It can't be permission issue for ASP.NET ASPNET or NETWORK SERVICE account because other files are working file.
I have opened iTextSharp.text.Image class and found that there are many overloaded methods for GetInstance(). I have resolved my problem using following code: note: catch block will run for problematic files.
如果您从 IIS 提供服务,请尝试回收应用程序池。 这为我解决了类似的图像上传“内存不足”错误。
If you're serving from IIS, try recycling the Application Pool. This solved a similar image upload "Out of Memory" error for me.
我创建了一个最小的表单示例,但仍然出现错误。
我尝试了使用 Image.FromFile() 的注释行以及使用 FileStream() 的行。 两者都会产生文件错误。
Image.FromFile() 错误是:
System.OutOfMemoryException:“内存不足”
filestream() 错误是:
System.UnaurthorizedAccessException:“访问路径‘E:\DCIM\100Canon\dsc_7218.jpg’被拒绝。
我在产生错误的行之前放置了一个断点,并且我能够使用 Windows 图像查看器打开图像文件。 然后我关闭了查看器,在前进到下一行并收到错误后,我无法再使用 Windows 查看器查看图像。 相反,我收到一条消息,指出我无权访问该文件。 我能够删除该文件。
此错误是可重复的。 我已经做过10多次了。 每次出现错误后,我都会删除用于 FileName 的文件。
所有文件均已验证未损坏。
我使用 Image.FromFile() 的原始代码在两年前编译时运行良好。 事实上,.exe 文件运行得很好。 我在代码的其他地方做了一个小的更改,并惊讶地发现如果没有这个错误,代码将无法编译。 我根据此页面上的信息尝试了 FileStream() 方法。
I created a minimal form example that still gives me errors.
I tried both the commented out line using Image.FromFile() as well as the line using FileStream(). Both produced file errors.
The Image.FromFile() error was:
System.OutOfMemoryException: 'Out of Memory'
The filestream() error was:
System.UnaurthorizedAccessException: 'Access to the path 'E:\DCIM\100Canon\dsc_7218.jpg' is denied.
I placed a Breakpoint just prior to the lines producing the error and I am able to open the image file using the Windows image viewer. I then closed the viewer and after I advanced to the next line and get the error, I can no longer view the image with the Windows viewer. Instead, I get a message that I do not have permission to access the file. I am able to delete the file.
This error is repeatable. I've done it over 10 times. Each time, after I get the error, I delete the file used for FileName.
All files were verified to be non-corrupt.
My original code that used Image.FromFile() worked fine when I compiled it 2 years ago. In fact, the .exe file runs just fine. I made a minor change somewhere else in the code and was surprised to find that the code would not compile without this error. I tried the FileStream() method based on the information on this page.
当您尝试打开 AVIF 或 WebP 等不受支持的格式时,也会引发此错误。
This error is also thrown when you are trying to open an unsupported format like AVIF or WebP.