C# "Parameter is not valid." creating new bitmap

if I try to create a bitmap bigger than 19000 px I get the error: Parameter is not valid. How can I workaround this??

System.Drawing.Bitmap myimage= new System.Drawing.Bitmap(20000, 20000);

Answers


Keep in mind, that is a LOT of memory you are trying to allocate with that Bitmap.

Refer to http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/37684999-62c7-4c41-8167-745a2b486583/

.NET is likely refusing to create an image that uses up that much contiguous memory all at once.

Slightly harder to read, but this reference helps as well:

http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.drawing/2005-06/msg00176.html

Each image in the system has the amount of memory defined by this formula:

bit-depth * width * height / 8

This means that an image 40800 pixels by 4050 will require over 660 megabytes of memory.


I suspect you're hitting memory cap issues. However, there are many reasons a bitmap constructor can fail. The main ones are listed in this Knowledge Base article talking about GDI+ limits in CreateBitmap. System.Drawing.Bitmap, internally, uses the GDI native API when the bitmap is constructed.

That being said, a bitmap of that size is well over a GB of RAM, and it's likely that you're either hitting the scan line size limitation (64KB) or running out of memory.


19000 pixels square, at 32bpp, would require 11552000000 bits (1.37 GB) to store the raster in memory. That's just the raw pixel data; any additional overhead inherent in the System.Drawing.Bitmap would add to that. Going up to 20k pixels square at the same color depth would require 1.5GB just for the raw pixel memory. In a single object, you are using 3/4 of the space reserved for the entire application in a 32-bit environment. A 64-bit environment has looser limits (usually), but you're still using 3/4 of the max size of a single object.

Why do you need such a colossal image size? Viewed at 1280x1024 res on a computer monitor, an image 19000 pixels on a side would be 14 screens wide by 18 screens tall. I can only imagine you're doing high-quality print graphics, in which case a 720dpi image would be a 26" square poster.


Set the PixelFormat when you new a bitmap, like:

new Bitmap(2000, 40000,PixelFormat.Format16bppRgb555)

and with the exact number above, it works for me. This may partly solve the problem.


Got this error when opening a TIF file. The problem was due to not able to open CMYK. Changed colorspace from RGB to CMYK and didn't get an error.

So I used taglib library to get image file size instead.

Code sample:

try
{
    var image = new System.Drawing.Bitmap(filePath);
    return string.Format("{0}px by {1}px", image.Width, image.Height);
}
catch (Exception)
{
    try
    {
         TagLib.File file = TagLib.File.Create(filePath);
         return string.Format("{0}px by {1}px", file.Properties.PhotoWidth, file.Properties.PhotoHeight);
    }
    catch (Exception)
    {
         return ("");
    }
}

Need Your Help

Converting an ArrayAdapter to CursorAdapter for use in a SearchView

android android-actionbar simplecursoradapter autocompletetextview android-cursoradapter

How can I convert an ArrayAdapter<String> of static data into a CursorAdapter for using Suggestion Listener in SearchView?

How do you execute a method in a class from the command line

php command-line

Basically I have a PHP class that that I want to test from the commandline and run a certain method. I am sure this is a basic question, but I am missing something from the docs. I know how to run ...