.net之Windows 窗体 "System.ArgumentException: Parameter is not valid"来自 Font.GetHeight(图形图形)
我支持使用 dotnet 3.5 和 ComponentFactory Krypton v4.4.0.0 的 winforms 应用程序,我最近实现了 AppDomain.CurrentDomain.UnhandledException 和 Application.ThreadException 处理程序来通知我客户端发生的异常,并发现了很多错误出现在日志中。这一刻让我费尽心思:
System.ArgumentException: Parameter is not valid.
at System.Drawing.Font.GetHeight(Graphics graphics)
at System.Drawing.Font.GetHeight()
at System.Drawing.Font.get_Height()
at System.Windows.Forms.Control.set_Font(Font value)
at System.Windows.Forms.DataGridViewComboBoxEditingControl.ApplyCellStyleToEditingControl(DataGridViewCellStyledataGridViewCellStyle)
at System.Windows.Forms.DataGridView.BeginEditInternal(Boolean selectAll)
at System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam,IntPtr lparam)
请注意,堆栈跟踪完全在 Windows 代码中。还有另一个通过我的类(class)之一:
System.ArgumentException: Parameter is not valid.
at System.Drawing.Font.GetHeight(Graphics graphics)
at System.Drawing.Font.GetHeight()
at System.Drawing.Font.get_Height()
at System.Windows.Forms.Control.set_Font(Font value)
at MyOrg.MyApp.WindowsClient.GuiControls.MaskedTextBoxEditingControl.ApplyCellStyleToEditingControl(DataGridViewCellStyledataGridViewCellStyle)
at System.Windows.Forms.DataGridView.BeginEditInternal(Boolean selectAll)
at System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
这是该片段的代码:
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
this.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment);
}
这并没有告诉我太多信息。
“System.ArgumentException:参数无效。”错误非常严重,让我无法继续下去,但是使用 dotPeek 我查看了 Font.Get_Height(Graphics g) 的代码并发现它是一个 GDI+ 错误,特别是 GetFontHeight:
public float GetHeight(Graphics graphics)
{
if (graphics == null)
{
throw new ArgumentNullException("graphics");
}
else
{
float size;
int fontHeight = SafeNativeMethods.Gdip.GdipGetFontHeight(new HandleRef((object) this, this.NativeFont), new HandleRef((object) graphics, graphics.NativeGraphics), out size);
if (fontHeight != 0)
throw SafeNativeMethods.Gdip.StatusException(fontHeight);
else
return size;
}
}
这是 GDI+ 方法: http://www.jose.it-berater.org/gdiplus/reference/flatapi/font/gdipgetfontheight.htm
我的状态错误是 Invalidparameter,如此处记录: http://msdn.microsoft.com/en-us/library/windows/desktop/ms534175(v=vs.85).aspx
不幸的是,这些都不能帮助我解决 Graphics 对象的问题。这是来自该领域用户未处理的异常。我最近发生了一次内存泄漏,这是由泄漏的 EventHandler 和消耗所有可用的 GDI 句柄引起的,但我已经修复了它,所以现在我不确定这是内存泄漏、GDI 句柄泄漏,还是只是不好在某处由用户执行异常操作触发的配置。
有人有什么想法吗?帮助非常感谢!
请您参考如下方法:
我刚遇到这个问题,花了几个小时才弄清楚,我将其追溯到代码库的其他部分,从控件中获取对字体的引用,然后对其执行 Dispose()字体。我能够通过创建一个 Windows 窗体项目、添加一个 TreeView 和一个 DataGridView 并在 TreeView 上执行它来重现这一点
treeView.Font.Dispose();
希望我的发现对遇到此问题的任何人都有帮助。
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。