Windows窗体来自Font.GetHeight(Graphics图形)的“ System.ArgumentException:参数无效”

mat*_*tao 3 .net gdi+ argumentexception winforms

我使用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)
Run Code Online (Sandbox Code Playgroud)

请注意,stacktrace完全在Windows代码中。我的一门课还有另外一门课:

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)
Run Code Online (Sandbox Code Playgroud)

这是该代码段的代码:

public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
  this.Font = dataGridViewCellStyle.Font;
  this.ForeColor = dataGridViewCellStyle.ForeColor;
  this.BackColor = dataGridViewCellStyle.BackColor;
  this.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment);
}
Run Code Online (Sandbox Code Playgroud)

这并不能告诉我很多。

“ 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;
  }
}
Run Code Online (Sandbox Code Playgroud)

这就是这种GDI +方法:http : //www.jose.it-berater.org/gdiplus/reference/flatapi/font/gdipgetfontheight.htm

而我的状态错误是Invalidparameter,如此处所述:http : //msdn.microsoft.com/zh-cn/library/windows/desktop/ms534175 (v=vs.85) .aspx

不幸的是,这些都没有帮助我解决Graphics对象的问题。这来自于现场用户未处理的异常。我最近有一个内存泄漏,它是由EventHandler泄漏和消耗了所有可用的GDI句柄引起的,但是我已经修复了这一点,所以现在我不确定这是内存泄漏,GDI句柄泄漏还是不好在某处配置由用户执行异常操作触发。

有人有想法么?帮助非常感谢!

Pes*_*nat 5

我只是碰到了这个问题,花了几个小时才弄清楚,我将其追溯到代码库的其他部分,以从控件中引用字体,然后在该字体上执行Dispose()。我可以通过创建Windows窗体项目,添加TreeView和DataGridView并在TreeView上执行此操作来重现此内容

treeView.Font.Dispose();

希望我发现的内容对遇到此问题的人有所帮助。