How to determine if image loading will throw an OutOfMemory exception in .NET?

I have an application written using .NET 3.5 SP1 that downloads images from an external site and displays them to end users. In rare cases, my users experience OutOfMemory errors because they upload huge images. Sometimes the source data associated with these images is large, but more often the size of the images is huge. I understand that I can never get around the fact that these OOM errors are thrown for certain images. It would be very helpful, however, if I could somehow determine if loading a particular image would lead to an OOM problem before I try to load the image.

Data for images is loaded into the stream, and then the image itself turns into System.Drawing.Image, making a call to System.Drawing.Image.FromStream (stream). I have no way to save these images to disk first. They must be loaded via memory.

If anyone has any tips or suggestions that would allow me to find that loading the image will result in an OOM exception, I would really appreciate it.

+5
source share
6 answers

You can use the MemoryFailPoint class to check for memory availability.

The best

+4
source

OutOfMemory , . , , , , .

, - . , .

+1

, : .NET ?

, ( ), . , , , , , .

, , , .

+1

. - , . , .

. , OOM, , . Windows. API HeapWalk() . MSDN. , .

, OOM - OOM, , . GDI +, . ", ".

-, , * * 4 > 550 32- . .

+1

, Content-Length HTTP, , , ...

+1

@Vagaus, , . , , , OOM - .

+1

All Articles