【发布时间】:2014-02-14 22:33:09
【问题描述】:
编辑 - 已解决?
重启 VS 后……异常似乎完全消失了。它只是不再发生。 Sooo...问题解决了吗?我想?
OP
在我的应用程序中,我正在尝试创建大小为1366 x 706 的位图。但是,当我尝试将其绘制到我的表单上时,它会返回一个 "parameter is not valid" 异常。
阅读后,我了解到参数错误通常意味着 C# 不会为位图分配足够的内存。但是,1366x706 似乎没有那么大的分辨率。
在磁盘上,1366x706 图像仅占用 2.5MB。是不是太大了WinForms 无法处理?
编辑
代码:
// 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;
}
如果您需要更多信息,请随时告诉我。
编辑 2
只要调整大小因子不等于 1.0,也会产生错误。
堆栈跟踪:
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
【问题讨论】:
-
大小当然不会太大。请出示您的代码。
-
将代码添加到 OP
-
哪一行会抛出错误?如果可以,请发布堆栈跟踪。
-
您检查将浮点数转换为整数的结果是否正确?
-
float resizeFactorX = 4.553333;不会编译,这使得其余部分也很可疑/不可靠。
标签: c# winforms bitmap resolution