39

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);
1
  • 3
    Specify a smaller PixelFormat? It defaults to 32bpp Commented Jun 13, 2011 at 17:08

5 Answers 5

42

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

Refer to https://web.archive.org/web/20130308000937/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:

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.

Sign up to request clarification or add additional context in comments.

Comments

9

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.

2 Comments

"Why do you need such a colossal image size?" A possible reason: He may be loading an image file into a Bitmap object, that he then wants to resize smaller.
2019 calling in. Looking a bit less collossal now - lets see how it goes in 2026
7

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.

Comments

6

I suspect you're hitting memory cap issues. However, there are many reasons a bitmap constructor can fail. The main reasons are 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.

Comments

0

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 ("");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.