What Is the Maximum Resolution of C# .Net Bitmap

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

This is a GDI+ limitation imposed by Windows. GDI+ creates a memory-mapped file view for the pixel data of the bitmap. That makes it very efficient, bitmaps tend to be large and the MMF helps to keep the pixel data out of the paging file. RAM pages can simply be discarded and re-read from the file. Also rather notorious, lots of programmers have seen their Save() call fail with a wonky exception when they forgot to dispose the old bitmap.

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

The size of a file mapping object that is backed by a named file is limited by disk space. The size of a file view is limited to the largest available contiguous block of unreserved virtual memory. This is at most 2 GB minus the virtual memory already reserved by the process.

"Largest available continuous block" is the restriction in a 32-bit process, tends to hover around ~600 MB, give or take. The 2 GB limit kicks in on a 64-bit process. Technically GDI+ could bypass this limit by remapping the view. But it doesn't, the LockBits() method (also heavily used internally) would be inefficient and very awkward to use.

To use larger bitmaps you need to move to the successor of GDI+, WIC (Windows Imaging Component). Exposed in .NET through the System.Windows.Media.Imaging namespace.

Bitmap maximum size before outofmemoryexception

This error was occurring on an MVC web application. It was caused by running a 32-bit IISEXPRESS server which forced the memory allocation of the bitmap to be contiguous instead of using any available chunks.

The solution was to force IISEXPRESS to run in 64-bit by going into the following Visual Studio settings :

TOOLS>OPTIONS>Projects and Solutions>WEB PROJECT and check the 64-bit IISEXPRESS box

Creating huge high-resolution Bitmap bigger than 23k x 23k

ARE YOU REALLY SURE YOU NEED SUCH SIZE? It should be bigger than than 150GB. Moreover if you consider a point size of 1/72 of inch your image will be 409 km 500 m...

EDITED If you need a high resolution image of a large area you should consider to use multiple images with different resolutions (think Google Maps for example).

Change DPI of Image object

The code creates a Bitmap destination. A Bitmap's resolution defaults to 96dpi x 96 dpi. As no other resolution is set the output file has the default resolution...

The answer to your question can be found in the reference manual.

https://msdn.microsoft.com/en-us/library/system.drawing.bitmap(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.setresolution(v=vs.110).aspx

public void SetResolution(
float xDpi,
float yDpi
)

Sets the resolution for this Bitmap.

If you would understand the code you copied from somewhere you would realize that you already had the answer to your question right in front of you...

source.SetResolution(original.HorizontalResolution, original.VerticalResolution);


Related Topics



Leave a reply



Submit