Out of memory on loading a Image with System.Drawi

2019-09-21 12:13发布

This question already has an answer here:

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?

2条回答
Fickle 薄情
2楼-- · 2019-09-21 12:49

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.

查看更多
Viruses.
3楼-- · 2019-09-21 12:53

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.

查看更多
登录 后发表回答