嵌入字体导致崩溃

时间:2013-05-04 13:07:14

标签: .net graphics fonts gdi+ embedded-resource

我有一个WinForm应用程序。我正在使用嵌入式资源中的自定义字体 它最初工作,但一段时间后程序崩溃 使用以下代码作为示例,如果我继续调整窗体大小,强制它不断重绘自身,它将在几秒钟内崩溃。我收到的消息是“Error in 'Form1_Paint()'. Object is currently in use elsewhere.” 我究竟做错了什么?我怎么能避免这种情况呢? 我从here.获得了字体 感谢。

Imports System.Drawing.Text
Imports System.Runtime.InteropServices

Public Class Form1
    Friend Harabara As Font

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadFonts()
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Try
            e.Graphics.DrawString("This was drawn using the custom font 'Harabara'", Harabara, Brushes.Lime, 10.0F, 10.0F)
        Catch ex As Exception
            MsgBox("Error in Form1_Paint()'" & vbCrLf & ex.Message)
        End Try
    End Sub

    Public Sub LoadFonts()
        Try
            Harabara = GetFontInstance(My.Resources.HarabaraHand, 24.0F, FontStyle.Italic)
        Catch ex As Exception
            MsgBox("Error in 'LoadFonts()'" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Function GetFontInstance(ByVal data() As Byte, ByVal Size As Single, ByVal Style As FontStyle) As Font
        Dim result As Font
        Try
            Dim pfc = New PrivateFontCollection
            'LOAD MEMORY POINTER FOR FONT RESOURCE
            Dim FontPtr As System.IntPtr = Marshal.AllocCoTaskMem(data.Length)
            'COPY THE DATA TO THE MEMORY LOCATION
            Marshal.Copy(data, 0, FontPtr, data.Length)
            'LOAD THE MEMORY FONT INTO THE PRIVATE FONT COLLECTION
            pfc.AddMemoryFont(FontPtr, data.Length)
            'FREE UNSAFE MEMORY
            Marshal.FreeCoTaskMem(FontPtr)

            result = New Font(pfc.Families(0), Size, Style)
            pfc.Families(0).Dispose()
            pfc.Dispose()
        Catch ex As Exception
            'ERROR LOADING FONT. HANDLE EXCEPTION HERE
            MsgBox("Error in 'GetFontInstance()'" & vbCrLf & ex.Message)
            result = New Font(FontFamily.GenericMonospace, 8)
        End Try
        Return result
    End Function
End Class

1 个答案:

答案 0 :(得分:2)

        Marshal.FreeCoTaskMem(FontPtr)

PrivateFontCollection的MSDN文档对此非常迟钝。但是,您需要为添加的字体保留内存,直到您无法再使用该字体为止。或者换句话说,AddMemoryFont()做制作字体的副本。因此,当程序尝试访问字体数据并被另一个非托管内存分配覆盖时,您的程序将会出现神秘的GDI +错误。

将FreeCoTaskMem()调用移动到FormClosed事件处理程序。或者,如果关闭表单也会终止您的程序,请不要理会。