C# “Parameter is not valid.” creating new bitmap

2019-01-03 03:16发布

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);

5条回答
够拽才男人
2楼-- · 2019-01-03 03:37

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 ("");
    }
}
查看更多
干净又极端
3楼-- · 2019-01-03 03:47

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.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-03 03:59

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.

查看更多
虎瘦雄心在
5楼-- · 2019-01-03 03:59

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.

查看更多
对你真心纯属浪费
6楼-- · 2019-01-03 04:04

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.

查看更多
登录 后发表回答