(C #) graphics.drawImage has size limit? How to deal with it?

I am trying to display a very large graphical representation of some data. I use a bitmap to permanently store the image and e.Graphics.DrawImage(myBitmap, new Point(0,0)) in the onPaint of the PictureBox control in my form. I noticed (and heard mentioned on other sites) that if my image has a height or width greater than 2 ^ 15, I get a Parameter not Valid exception, but I have not found the official documentation for this limit.

Is this 2 ^ 15 image size limitation defined by the official Graphics.DrawImage ? Are there any simple ways to get around my image in the form?

(Yes, the pictureBox is set to the same size as the image, or larger. Side question: should I just use onPaint of the form itself instead of the picture window?)

+4
source share
3 answers

You have a few questions. Firstly, it is not fundamentally possible to display an image so large (if you do not have any insanely huge monitor or several monitor settings) without reducing it to a size that will correspond to a normal screen.

You can reduce this size using Graphics.DrawImage , but keep in mind that even when using high-quality InterpolationMode interpolation is performed by no more than a few neighboring pixels, which means that there is a limit on the maximum number, you can reduce the image without serious loss of information (usually up to 25%).

Secondly, a .Net Bitmap object is much more complex than a simple pixel array. Raster images are sometimes created in the video memory instead of the general program memory, which more strictly limits their maximum size (in a compact structure, as one example, one of the Bitmap designers creates pixel data in the video memory, which is limited to 4 MB instead of 32 MB, which is usually available for the process. NET). As a result, there is no documented maximum size for Bitmap - this is internal implementation information (also affected by any existing bitmaps) that the programmer can only find with difficulty, having received an exception if it is too large. Therefore, using Bitmap to store an arbitrarily large set of data points will not work.

Your best approach here, probably, is to store your data as a regular two-dimensional array (possibly of type int[,] ), which can be arbitrarily large without throwing an OutOfMemoryException (although not forcing your computer to go swapfile- crazy), and then write your own method that copies from this array to the actual (and practically size) Bitmap . Then you would copy Bitmap from this to your PictureBox (or, simply put, just set this Bitmap as the Image property) - it is best to avoid the Paint method when possible).

+5
source

You will have a problem long before you reach this limit, about 10000x10000 pixels you will use almost all of your memory for your bitmap. Consider that the internal gdi + bitmap will be 32bppargb, and you are counting on 4 bytes per pixel x 100000000 = 4 GB.

You need to tile the image, or if you draw it manually, follow the paging solution.

+2
source

I managed to build bitmaps up to 25200 by 15000 pixels using C # forms and windows in a 64-bit application. I'm still experimenting, but it seems like this is a bitmap size limit.

+1
source

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


All Articles