【问题标题】:VB.Net Metafile generates error ("A generic error occurred in GDI+")VB.Net 元文件生成错误(“GDI+ 中发生一般错误”)
【发布时间】:2020-10-29 18:11:27
【问题描述】:

我想从你那里得到一点帮助。我正在开发一个可以多次调用的图表创建器。每次的图表都与早期版本不同,但我想使用相同的文件名。问题是,当我单击按钮时,程序会在图片框中显示图表表单,但是如果表单已关闭并且我再次单击按钮,则会出现错误(“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

【问题讨论】:

  • 如果您每次都使用不同的文件名,是否可以正常工作?

标签: vb.net gdi+ metafile


【解决方案1】:

你需要在关闭Form Diagram_FORM时释放Diagram_FORM.PictureBox1.Image,否则下次在类Diagram的方法create_diagram()中使用相同的文件名c:\temp\test.wmf初始化Metafile时会出现异常:

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

【讨论】:

    猜你喜欢
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    相关资源
    最近更新 更多