6

I'm trying to load an extremely large image (14473x25684), but I'm hitting into a memory limitation.

Here's a simple program to demonstrate the problem:

static void Main(string[] args)
{
    string largeimage = @"C:\Temp\test_image.jpg"; // 14473x25684

    Image i = Bitmap.FromFile(largeimage); // OutofMemoryException was unhandled
}

Now I understand that the issue isn't relevant to how much physical memory I have, but rather is an addressing limitation. Is there anything I can do to get around this limitation?

The image is indeed valid and it opens fine in Photoshop (VM Size: 916MB) and ACDSee. Also don't bother to Google the dimensions as the dimensions listed aren't exact. :)

Thank you for your time.

Generic Comrade
  • 327
  • 2
  • 7
  • I've run into this before. From what I can tell it's the framework kicking over. – Chuck Conway Aug 24 '10 at 04:49
  • Wow. That's a big picture. Unfortunately, the framework Bitmap class won't cover your needs. You may find that taking a chunked approach, where you marshal the bitmap header yourself and sample the pixels into a more manageable image dimension is your best bet. It's a challenge though, as it involves a little wheel-rebuilding. I'd be surprised if Photoshop and ACDSee load the entire uncompressed image into memory. – kbrimington Aug 24 '10 at 05:10

2 Answers2

4

The Bitmap class will require around 1.5GB of memory to hold that instance. The .NET memory allocator normally chokes around the 1GB mark.

leppie
  • 115,091
  • 17
  • 196
  • 297
0

OS can not allocate contiguous amount of memory. All you can do about that is use of MemoryFailPoint and catch InsufficientMemoryException. But this only save you from app crashing.
As for me, to open such a big file you should use binary reader and draw a file via System.Drawing.

here is good question and answers When is it OK to catch an OutOfMemoryException and how to handle it?

Community
  • 1
  • 1
Sergey Mirvoda
  • 3,209
  • 2
  • 26
  • 30