Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I get this exception:

System.ArgumentException occurred Message="Parameter is not valid." Source="System.Drawing" StackTrace: at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format) at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)...

when I instantiate a new Bitmap in this way:

C#
Bitmap resImage = new Bitmap(width, height); 
resImage.SetResolution(600, 600);


where width and height are the dimensions converted in pixel for 600 dpi of the size set by the user in cm, which is in this specific case 28x41 cm (A3 format image) corresponding to 6614x9685 pixel. After that I will create a graphics object from the bitmap and draw on it with the methods of GDI+. I need an high resolution image because it is going to be added to a pdf document and I want a great quality result.

I get this exception both from a windows application and a web one, but because it is a continous process of generating this kind of bitmaps, the exception is thrown after a different times of reiteration according to the type of application and the dimension of the image. In particular, I obtain the error after an order of hundreths of images produced in the windows app and tenths in the web app.

I have already dispose all the graphics objects and now I really don't know what to do!

Why I obtain this exception? How can I solve the problem?

Thanks in advance for every help!
Posted
Updated 11-Nov-19 2:06am
Comments
johannesnestler 8-Nov-13 4:39am    
sounds like you are setting <= 0 as width or height, have you verified (debugged) the actual values when you get that exception?
Sara Noemi 8-Nov-13 4:58am    
Yes, the values are actually 6614x9685 pixel; I've verified them in debugging.
johannesnestler 8-Nov-13 5:04am    
hmm sounds quite strange that you see the error only sometimes, sorry have no other idea to help you at the moment :-(
Sara Noemi 8-Nov-13 5:44am    
It seems that after a few loop of creation of the bitmap there is a problem of overflow of memory or something like that..

Check your values - I just tried in in my code:
C#
int width = 6614;
int height = 9685;
Bitmap resImage = new Bitmap(width, height);
resImage.SetResolution(600, 600);
And I don't get an exception.
So, most likely, the values you are passing in for width and height are not what you think - have a look in the debugger and check exactly what pixel values you are passing to the bitmap constructor.

Then single step your code - as I say, it works here.
 
Share this answer
 
Comments
Sara Noemi 8-Nov-13 5:34am    
I'm absolutely sure that the values are correct; in fact the bitmap is created some times as I said and after some loop of creation I obtain the error. Have you tried that code just one time or in a repeated loop?
OriginalGriff 8-Nov-13 5:49am    
Memory.
This is going to be classed as a "Large Object" - which means it goes into the Large Object Heap rather than the regular heap - and that is managed differently. Are you manually calling Dispose on these objects when you are finished with them?
Sara Noemi 8-Nov-13 6:01am    
I understand that probably it is a matter of memory. I call the Dispose explicity for all the graphics object that I used and so I can't understand what is wrong! I'm going crazy with it!
OriginalGriff 8-Nov-13 6:18am    
I tried this added to a button click for an app I'm working on
for (int i = 0; i < 100; i++)
{
int width = 6614;
int height = 9685;
Bitmap resImage = new Bitmap(width, height);
resImage.SetResolution(600, 600);
}
And it failed regularly after 5 iterations.
Adding the dispose:
for (int i = 0; i < 100; i++)
{
int width = 6614;
int height = 9685;
Bitmap resImage = new Bitmap(width, height);
resImage.SetResolution(600, 600);
resImage.Dispose();
}
And it made it through the hundred every time. So what am I doing that is different to you?
(Bear in mind that this isn't a "massive" bitmap, although it does take 250Mb of memory each: I've worked with bigger at 15519 x 19988 without problems, though I wouldn't want to create many of those at one time! :laugh: )
are you trying to create bitmap from stream? if yes, please check your stream is seek-able or not.

C#
if (!strmftpdata.CanSeek)
{
}


if it is not seek-able then you need to make it seek-able.
 
Share this answer
 
Comments
Sara Noemi 8-Nov-13 5:32am    
How can I create the bitmap from stream? I instantiate this Bitmap and then create a graphics from it and use GDI+ method to draw on it. It is not a bitmap loaded from a file.
Yep, instantiate bitamp with stream. for example.

C#
public static void saveimage(System.IO.Stream InputFileStream, string ImageName, string ImageExtension)
        {

            Bitmap bmpBitMapImage = new Bitmap(InputFileStream, true);}



I have also faced this error so many times.
 
Share this answer
 
Comments
Sara Noemi 8-Nov-13 5:58am    
But what is the InputFileStream if I don't have a file from which instantiate the bitmap?
Mishra Laxmikant 8-Nov-13 6:07am    
its nothing just
FileUpload file_ = new FileUpload();
file_.PostedFile.InputStream
Dear friend!
Did you solve this problem?
I have the same problem as you.
Could you tell me how to deal with this problem?
Thank you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900