none
System.Drawing.Bitmap max size RRS feed

  • Question

  • I've got a situation where I'm creating a bitmap as follows:

    bitmap=new Bitmap(newWidth,newHeight);

    newWidth is say 10,000 and newHeight is like 9,000;

    Upon hitting this line I get an error about invalid parameter being used. It happens almost everytime, but a few times I've been able to create the bitmap. I'm using .net 1.1.4322 for this project not 2.0 so this might not be an issue anymore. Is there a limitation to bitmap and what is it dependent upon?
    Friday, February 2, 2007 6:52 PM

Answers

  • Yeah, you're pushing the envelope a bit with this bitmap.  You'll need 10000 x 9000 x 4 = 360 MB of contiguous virtual memory space for it.  I had no problem allocating it myself within the debugger, 1 GB Ram, 2.5 GB swap.  But you're probably allocating some other large buffer too to process the PDF.  Or are bringing in some DLL whose preferred load address fragments the virtual memory space.  DependencyWalker can help you diagnose that.

    Note that you can limit the allocated size of the bitmap by choosing a more conservative pixel format.  Format24bppRgb will need 270 MB.
    Saturday, February 3, 2007 3:05 PM
    Moderator

All replies

  • My swap is set to 3gb. I have 2gb of ram and about 40gb free hd space.

    My ultimate reason for trying to create such a large bit map is that I have a pdf coming in which is that large (a bunch of print banners) and I need to convert it to a jpeg. So I bring in the pdf with Tallcomponents PDF Rasterizer which will draw it to a System.Drawing.Graphics object which has been made from the bitmap. Like so:

                    Bitmap bitmap=new Bitmap(newWidth,newHeight); <<<<CODE FAILS HERE
                    bitmap.SetResolution(newRes,newRes);
                    Graphics graphics = Graphics.FromImage(bitmap);
                    graphics.SmoothingMode=SmoothingMode.AntiAlias;
                    graphics.ScaleTransform(scale,scale);

                    page.Draw(graphics);

                    ImageFormat imgFormat=getFormat(format);
                    if(imgFormat==ImageFormat.Jpeg)
                    {
                        EncoderParameters ep=new EncoderParameters(1);
                        ep.Param[0]=new EncoderParameter(Encoder.Quality,80L);
                        bitmap.Save(output,jpegCodecInfo,ep);
                    }
                    else
                        bitmap.Save(output,imgFormat);
    Friday, February 2, 2007 7:18 PM
  • Wild guess since I'm not an expert on the GDI/GDI+ functions:

    It may be that there isn't that much contigous memory available, which would lead to a out of memory exception/allocation failure.

    http://msdn.microsoft.com/msdnmag/issues/06/11/CLRInsideOut/ could be a good resource to start looking for memory issues in managed apps...

    Best regards,
    Johan Stenberg

    Saturday, February 3, 2007 4:26 AM
    Moderator
  • Yeah, you're pushing the envelope a bit with this bitmap.  You'll need 10000 x 9000 x 4 = 360 MB of contiguous virtual memory space for it.  I had no problem allocating it myself within the debugger, 1 GB Ram, 2.5 GB swap.  But you're probably allocating some other large buffer too to process the PDF.  Or are bringing in some DLL whose preferred load address fragments the virtual memory space.  DependencyWalker can help you diagnose that.

    Note that you can limit the allocated size of the bitmap by choosing a more conservative pixel format.  Format24bppRgb will need 270 MB.
    Saturday, February 3, 2007 3:05 PM
    Moderator