vb.net - VB.Net 元文件生成错误(“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
图表类:
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.PictureBox1.Image
关闭 Form 时需要 dispose ,否则下次在类的方法中使用相同的文件名Diagram_FORM
初始化时会出现异常:Metafile
c:\temp\test.wmf
create_diagram()
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
推荐阅读
- neo4j - gds.alpha.shortestPath.stream 中的节点属性
- logic - 如何找到给定数组的后缀数组和前缀数组?
- image - 如果我们沿着一个轴旋转相机,图像是如何失真的
- html - 如何在没有 JS 的情况下让我的幻灯片重复播放?
- c++ - 如何防止在 for 循环结束之前在 C 中换行;
- flutter - Flutter 单元测试中的断言错误失败
- javascript - offie-js Word Web 插件:可以动态添加菜单项
- install4j - 使用 Install4J 生成 Windows 可执行文件,JDK11 单独工作
- python - 是否可以在 manim 的图形对象上使用 ReplacementTransform?
- javascript - Prevent Multiple Instances for SetInterval