What is the maximum resolution of C # .NET Bitmap?

Theoretically, it should be 65 535 x 65 535, if you have enough memory, about 17 GB.

However, by creating the .NET 4.5 Console application to test it, it throws a System.ArgumentException: the parameter is invalid.

The application is built for a 64-bit platform. It works on a 64-bit platform with 32 GB of memory. The maximum resolution I was able to get is 22,000 x 22,000 pixels.

I could not find documentation on this.

And the strange behavior is that even with 22,000 x 22,000 pixels, this does not always work. Sometimes it fires, and it sometimes throws an exception. This makes me think that this is due to the continuous distribution of memory, but there is about 30 GB of free memory.

Does anyone have any experience with this? And if I wanted to work, say, 100,000 x 100,000 pixels or more, what would be a better way than implementing my own bitmap?

EDIT: The problem is not the .NET Maximum Object Size. This can be overcome by targeting on 64-bit platforms and setting the gcAllowVeryLargeObjects flag in the application configuration. That way, I can make the application consume more than 15 GB of memory with a single array of integers. So far, the answer seems to lie in the base implementation of GDI +, but how do I get around it?

+6
source share
2 answers

This is a GDI + limitation imposed by Windows. GDI + creates a memory card view for the pixel data of the bitmap image. This makes it very efficient, bitmaps are usually large, and MMF helps save pixel data from the page file. RAM can simply be dropped and re-read from the file. It's also notorious that many programmers saw that their Save () call failed with a random exception when they forgot to recycle the old bitmap.

Windows limits how large the view on the MMF can be, in other words, the amount of data in the file that can be directly addressed, as described in this MSDN article :

The size of the file association object that is supported by the named file is limited by disk space. The file view size is limited by the largest available contiguous block of unreserved virtual memory. This is no more than 2 GB minus virtual memory already reserved by the process.

"The largest available continuous block" is a limitation in a 32-bit process, it tends to hover around 600 MB, give or receive. The 2 GB limit is triggered by a 64-bit process. Technically, GDI + can get around this limit by reassigning the view. But this is not so, the LockBits () method (also heavily used internally) will be inefficient and very inconvenient to use.

To use larger bitmaps, you need to go to the successor to GDI +, WIC (a component of Windows Imaging ). Opens in .NET through the System.Windows.Media.Imaging namespace.

+13
source

You are using the maximum allowed object size inside .net. This is described here: A very large collection in .Net raises a memory exception

Edit: It looks like you are using the GDI Plus restriction. Hans answer can provide you an alternative. If you can live within the limits, then my answer may give some recommendations.

Knowing that you can calculate the largest bitmap you can create.

  • Maximum object size is 2 GB: 2,147,483,648
  • The default bitmap is 32bpp (4 bytes), the largest area we can have is 2GB / 4 = 536 870 912
  • If we need a square, then the largest of them can be sqrt (2GB / 4) = 23.170

So, the following code works fine:

Bitmap b = new Bitmap(23170,23170); 

But the following is true:

 Bitmap b = new Bitmap(23171,23170); 

If you want to save a larger image, you have to change the pixel format to a smaller bpp:

 Bitmap b = new Bitmap(65535,65535, PixelFormat.Format4bppIndexed); 
+6
source

Source: https://habr.com/ru/post/984066/


All Articles