Reputation: 193
EDIT - Solved?
After restarting VS... The exception seems to have COMPLETELY dissappeared. It just doesn't occur anymore. Sooo... Problem solved? I suppose?
OP
In my application, I am trying to create a bitmap of size 1366 x 706
. However, when I attempt to paint it onto my form, it returns a "parameter is not valid"
exception.
After reading up, I learned that the parameter error usually means that C# won't allocate enough memory for the bitmap. However, 1366x706
doesn't seem to be that large of a resolution.
On disk, a 1366x706
image only takes up 2.5MB
. Is that too large for WinForms
to handle?
EDIT
Code:
// These variables vary based on the size of the winform, these values return the error
float resizeFactorX = 4.553333f;
float resizeFactorY = 2.353333f;
// The original size of the image is ALWAYS 300x300, that never changes
public static Image resizeImageByFactors(Image i, float resizeFactorX, float resizeFactorY)
{
Bitmap bitmap1 = new Bitmap((int)((float)(i.Width) * resizeFactorX), (int)((float)(i.Height * resizeFactorY)));
using (var gr = Graphics.FromImage(bitmap1))
{
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
gr.DrawImage(i, new Rectangle(Point.Empty, new Size(bitmap1.Width, bitmap1.Height)));
}
return bitmap1;
}
If you need any more information, don't hesitate to let me know.
EDIT 2
The error is also produced anytime the resize factors are not equal to 1.0.
Stack Trace:
System.ArgumentException: Parameter is not valid.
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
at Paint_Test.Form1.resizeImageByFactors(Image i, Single resizeFactorX, Single resizeFactorY) in c:\Users\ApachePilotMPE\Documents\Visual Studio 2012\Projects\Paint Test\Paint Test\Form1.cs:line 273
Upvotes: 2
Views: 696
Reputation: 5294
"Either way, the posted code doesn't produce the error for your audience. Either the numbers are out of whack or your image isn't valid. Something we can't see. – LarsTech"
Well, it seems that your error have nothing to do with the code that you have posted here.
I think it is related with how you are calculating the XY resize factors. If you are still doing this:
resizeFactorX = (float)(this.ClientSize.Width / 300.0);
You will get 0 if Width < 300!
As you said:
"I've found that the application throws this error anytime a bitmap larger than the ClientSize of the form is created"
This happens because Width is an int, and you are casting after the int/float division, that results an int. You must cast the int to float BEFORE the division.
Correct way:
resizeFactorX = ((float)this.ClientSize.Width) / 300;
I hope this solves your problem. :)
Upvotes: 1