VB.Net 图元文件生成错误 ("A generic error occurred in GDI+")
VB.Net Metafile generates error ("A generic error occurred in GDI+")
我想得到你的一点帮助。我正在研究可以多次调用的图表创建器。图表每次都与早期版本不同,但我想使用相同的文件名。问题是,当我单击按钮时,程序显示的是带有图片框中图表的图表表单,但是如果表单已关闭并且我再次单击该按钮,则会出现错误(“GDI+ 中发生一般错误” ).我认为 mf.dispose() 不会关闭文件并打开它。你觉得是什么问题,我该如何解决?
主要形式:
Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Diagram.create_diagram()
Diagram_FORM.PictureBox1.Image = New Metafile("c:\temp\test.wmf")
Diagram_FORM.Show()
End Sub
图Class:
Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging
Class Diagram
Public Sub create_diagram()
Dim diagram_width As Integer = 600
Dim diagram_Height As Integer = 600
Dim filename As String = "c:\temp\test.wmf"
Dim gr As Graphics
gr = Graphics.FromImage(New Bitmap(diagram_width, diagram_Height))
' Make a Graphics object so we can use its hDC as a reference.
Dim hdc As IntPtr = gr.GetHdc
' Make the Metafile, using the reference hDC.
Dim bounds As New RectangleF(0, 0, Diagram_WidthSize, Diagram_HeightSize)
Dim mf As New Metafile(filename, hdc, bounds, MetafileFrameUnit.Pixel)
gr.ReleaseHdc(hdc)
' Make a Graphics object and draw.
gr = Graphics.FromImage(mf)
gr.PageUnit = GraphicsUnit.Pixel
gr.Clear(Color.White)
draw_diagram_background(gr)
draw_diagram_curve(gr)
gr.Dispose()
mf.Dispose()
End Sub
Private Sub draw_diagram_background(Byval gr as Graphics)
'some code
End Sub
Private Sub draw_diagram_curve(Byval gr as Graphics)
'some code
End Sub
End Class
关闭表单Diagram_FORM
时需要处理Diagram_FORM.PictureBox1.Image
,否则下次在方法[=]中初始化Metafile
相同文件名c:\temp\test.wmf
时会出现异常16=] 共 class Diagram
:
Dim mf As New Metafile(filename, hdc, bounds, MetafileFrameUnit.Pixel)
您可以在您的 Diagram_FORM
表单中添加以下代码,以便在您关闭它时处理它 PictureBox1.Image
。
Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
If PictureBox1.Image IsNot Nothing Then
PictureBox1.Image.Dispose()
End If
MyBase.OnFormClosed(e)
End Sub
我想得到你的一点帮助。我正在研究可以多次调用的图表创建器。图表每次都与早期版本不同,但我想使用相同的文件名。问题是,当我单击按钮时,程序显示的是带有图片框中图表的图表表单,但是如果表单已关闭并且我再次单击该按钮,则会出现错误(“GDI+ 中发生一般错误” ).我认为 mf.dispose() 不会关闭文件并打开它。你觉得是什么问题,我该如何解决?
主要形式:
Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Diagram.create_diagram()
Diagram_FORM.PictureBox1.Image = New Metafile("c:\temp\test.wmf")
Diagram_FORM.Show()
End Sub
图Class:
Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging
Class Diagram
Public Sub create_diagram()
Dim diagram_width As Integer = 600
Dim diagram_Height As Integer = 600
Dim filename As String = "c:\temp\test.wmf"
Dim gr As Graphics
gr = Graphics.FromImage(New Bitmap(diagram_width, diagram_Height))
' Make a Graphics object so we can use its hDC as a reference.
Dim hdc As IntPtr = gr.GetHdc
' Make the Metafile, using the reference hDC.
Dim bounds As New RectangleF(0, 0, Diagram_WidthSize, Diagram_HeightSize)
Dim mf As New Metafile(filename, hdc, bounds, MetafileFrameUnit.Pixel)
gr.ReleaseHdc(hdc)
' Make a Graphics object and draw.
gr = Graphics.FromImage(mf)
gr.PageUnit = GraphicsUnit.Pixel
gr.Clear(Color.White)
draw_diagram_background(gr)
draw_diagram_curve(gr)
gr.Dispose()
mf.Dispose()
End Sub
Private Sub draw_diagram_background(Byval gr as Graphics)
'some code
End Sub
Private Sub draw_diagram_curve(Byval gr as Graphics)
'some code
End Sub
End Class
关闭表单Diagram_FORM
时需要处理Diagram_FORM.PictureBox1.Image
,否则下次在方法[=]中初始化Metafile
相同文件名c:\temp\test.wmf
时会出现异常16=] 共 class Diagram
:
Dim mf As New Metafile(filename, hdc, bounds, MetafileFrameUnit.Pixel)
您可以在您的 Diagram_FORM
表单中添加以下代码,以便在您关闭它时处理它 PictureBox1.Image
。
Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
If PictureBox1.Image IsNot Nothing Then
PictureBox1.Image.Dispose()
End If
MyBase.OnFormClosed(e)
End Sub