System.drawing.graphics 的内存不足异常错误
Out Of memory exception error for System.drawing.graphics
我有一个 vb.net 应用程序,它使用 system.drawing 在组合框 dropdown.But 中显示多个数据字段 dropdown.But 每次打开它时它都会崩溃并收到错误消息未处理的异常(出于内存)
System.OutOfMemoryException: Out of memory. at
System.Drawing.Graphics.FromHdcInternal(IntPtr hdc) at
System.Windows.Forms.ComboBox.WmReflectDrawItem(Message& m) at
System.Windows.Forms.ComboBox.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)
为下拉菜单绘制代码
Private Sub NamesComboBox_DrawItem2(sender As Object, g As System.Windows.Forms.DrawItemEventArgs) Handles Grower.DrawItem
Grower.DrawMode = DrawMode.OwnerDrawVariable
Grower.Size = New System.Drawing.Size(75, 120)
Grower.DropDownStyle = ComboBoxStyle.DropDown
Grower.DropDownWidth = 500
g.DrawBackground()
Dim drv As DataRowView = CType(Grower.Items(g.Index), DataRowView)
' Retrieve the value of each column.
Dim Grow As String = drv("Grower").ToString()
Dim Ptype As String = drv("Poultry Type").ToString()
Dim GrowName As String = drv("Grower Name").ToString()
Dim BoardAccount As String = drv("Board Account").ToString()
Dim ChickAccount As String = drv("Chick Account").ToString()
Dim CatchingAccount As String = drv("Catching Account").ToString()
' Get the bounds for the first column
Dim a1 As Rectangle = g.Bounds
a1.Width = 50
' Draw the text on the first column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(Grow, g.Font, sb, a1)
End Using
' Draw a line to isolate the columns
Using p As Pen = New Pen(Color.Black)
g.Graphics.DrawLine(p, a1.Right, 0, a1.Right, a1.Bottom)
End Using
' Get the bounds for the second column
Dim a2 As Rectangle = g.Bounds
a2.X = a1.Width + 1
a2.Width = 15
' Draw the text on the second column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(Ptype, g.Font, sb, a2)
End Using
Using p As Pen = New Pen(Color.Black)
g.Graphics.DrawLine(p, a2.Right, 0, a2.Right, a2.Bottom)
End Using
' Get the bounds for the third column
Dim a3 As Rectangle = g.Bounds
a3.X = 65
a3.Width = 180
' Draw the text on the third column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(GrowName, g.Font, sb, a3)
End Using
Using p As Pen = New Pen(Color.Black)
g.Graphics.DrawLine(p, a3.Right, 0, a3.Right, a3.Bottom)
End Using
' Get the bounds for the third column
Dim a4 As Rectangle = g.Bounds
a4.X = 245
a4.Width = 50
' Draw the text on the third column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(BoardAccount, g.Font, sb, a4)
End Using
Using p As Pen = New Pen(Color.Black)
g.Graphics.DrawLine(p, a4.Right, 0, a4.Right, a4.Bottom)
End Using
' Get the bounds for the third column
Dim a5 As Rectangle = g.Bounds
a5.X = 295
a5.Width = 50
' Draw the text on the third column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(ChickAccount, g.Font, sb, a5)
End Using
Using p As Pen = New Pen(Color.Black)
g.Graphics.DrawLine(p, a5.Right, 0, a5.Right, a5.Bottom)
End Using
' Get the bounds for the third column
Dim a6 As Rectangle = g.Bounds
a6.X = 345
a6.Width = 50
' Draw the text on the third column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(CatchingAccount, g.Font, sb, a6)
End Using
End Sub
paint 方法不应更改控件的任何属性,因此将这些行移至窗体的构造函数或 OnLoad 覆盖:
Grower.DrawMode = DrawMode.OwnerDrawVariable
Grower.Size = New System.Drawing.Size(75, 120)
Grower.DropDownStyle = ComboBoxStyle.DropDown
Grower.DropDownWidth = 500
从您的代码中不清楚是否需要 OwnerDrawVariable。尝试使用 OwnerDrawFixed 并根据自己的喜好设置 ItemHeight 属性。此外,ComboBox 没有可调整的高度,因此您只能设置宽度。
我有一个 vb.net 应用程序,它使用 system.drawing 在组合框 dropdown.But 中显示多个数据字段 dropdown.But 每次打开它时它都会崩溃并收到错误消息未处理的异常(出于内存)
System.OutOfMemoryException: Out of memory. at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc) at System.Windows.Forms.ComboBox.WmReflectDrawItem(Message& m) at System.Windows.Forms.ComboBox.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)
为下拉菜单绘制代码
Private Sub NamesComboBox_DrawItem2(sender As Object, g As System.Windows.Forms.DrawItemEventArgs) Handles Grower.DrawItem
Grower.DrawMode = DrawMode.OwnerDrawVariable
Grower.Size = New System.Drawing.Size(75, 120)
Grower.DropDownStyle = ComboBoxStyle.DropDown
Grower.DropDownWidth = 500
g.DrawBackground()
Dim drv As DataRowView = CType(Grower.Items(g.Index), DataRowView)
' Retrieve the value of each column.
Dim Grow As String = drv("Grower").ToString()
Dim Ptype As String = drv("Poultry Type").ToString()
Dim GrowName As String = drv("Grower Name").ToString()
Dim BoardAccount As String = drv("Board Account").ToString()
Dim ChickAccount As String = drv("Chick Account").ToString()
Dim CatchingAccount As String = drv("Catching Account").ToString()
' Get the bounds for the first column
Dim a1 As Rectangle = g.Bounds
a1.Width = 50
' Draw the text on the first column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(Grow, g.Font, sb, a1)
End Using
' Draw a line to isolate the columns
Using p As Pen = New Pen(Color.Black)
g.Graphics.DrawLine(p, a1.Right, 0, a1.Right, a1.Bottom)
End Using
' Get the bounds for the second column
Dim a2 As Rectangle = g.Bounds
a2.X = a1.Width + 1
a2.Width = 15
' Draw the text on the second column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(Ptype, g.Font, sb, a2)
End Using
Using p As Pen = New Pen(Color.Black)
g.Graphics.DrawLine(p, a2.Right, 0, a2.Right, a2.Bottom)
End Using
' Get the bounds for the third column
Dim a3 As Rectangle = g.Bounds
a3.X = 65
a3.Width = 180
' Draw the text on the third column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(GrowName, g.Font, sb, a3)
End Using
Using p As Pen = New Pen(Color.Black)
g.Graphics.DrawLine(p, a3.Right, 0, a3.Right, a3.Bottom)
End Using
' Get the bounds for the third column
Dim a4 As Rectangle = g.Bounds
a4.X = 245
a4.Width = 50
' Draw the text on the third column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(BoardAccount, g.Font, sb, a4)
End Using
Using p As Pen = New Pen(Color.Black)
g.Graphics.DrawLine(p, a4.Right, 0, a4.Right, a4.Bottom)
End Using
' Get the bounds for the third column
Dim a5 As Rectangle = g.Bounds
a5.X = 295
a5.Width = 50
' Draw the text on the third column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(ChickAccount, g.Font, sb, a5)
End Using
Using p As Pen = New Pen(Color.Black)
g.Graphics.DrawLine(p, a5.Right, 0, a5.Right, a5.Bottom)
End Using
' Get the bounds for the third column
Dim a6 As Rectangle = g.Bounds
a6.X = 345
a6.Width = 50
' Draw the text on the third column
Using sb As SolidBrush = New SolidBrush(g.ForeColor)
g.Graphics.DrawString(CatchingAccount, g.Font, sb, a6)
End Using
End Sub
paint 方法不应更改控件的任何属性,因此将这些行移至窗体的构造函数或 OnLoad 覆盖:
Grower.DrawMode = DrawMode.OwnerDrawVariable
Grower.Size = New System.Drawing.Size(75, 120)
Grower.DropDownStyle = ComboBoxStyle.DropDown
Grower.DropDownWidth = 500
从您的代码中不清楚是否需要 OwnerDrawVariable。尝试使用 OwnerDrawFixed 并根据自己的喜好设置 ItemHeight 属性。此外,ComboBox 没有可调整的高度,因此您只能设置宽度。