Out of memory on loading a Image with System.Drawing.Image [duplicate]

馋奶兔 提交于 2019-12-13 10:59:47

问题


System.Drawing.Image original = System.Drawing.Image.FromFile(photo);

That's the code where i have the problem "Out of memory."

"photo" is a path of an image which the size is 6400x6400.

Have anyone idea about that?


回答1:


Is this C#? I think the answer may be here.

out of memory Image.FromFile

Top Answer from user: https://stackoverflow.com/users/31158/jordao

In the Image.FromFile documentation, an OutOfMemoryException can be throw if:

The file does not have a valid image format.

-or-

GDI+ does not support the pixel format of the file.

Check your image format.

Also, if you want to close the stream right after loading the image, you must make a copy of the image. Take a look here. GDI+ must keep the stream open for the lifetime of the image.

Another top answer and thread to consider then:

Out Of Memory exception on System.Drawing.Image.FromFile()

From user: https://stackoverflow.com/users/22656/jon-skeet

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?

Also, I would add- have you saved this file out using your code and tried to view it in another image viewer? It may be corrupt if it is being created incorrectly.




回答2:


Since I assume this is in C#, is your code is in a loop or in the Paint event? This could explain why it is not working. If you load several images or a single image multiple times, you will of course encounter a "Out of memory" Exception.



来源:https://stackoverflow.com/questions/16842708/out-of-memory-on-loading-a-image-with-system-drawing-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!