0

我在关注这个:http: //zerosandtheone.com/blogs/vb/archive/2009/11/20/vb-net-include-a-font-as-an-embedded-resource-in-your-application.aspx允许我的应用程序在标签中使用自定义字体。问题是我可以在我的计算机上运行应用程序(可能是因为我安装了这个字体),当任何其他人在他的计算机上运行编译的应用程序时就会出现问题;出现异常捕获中的以下错误:53 File doesn't exists

这个异常在哪里?

我说的是我上面链接的模块:

'MATTHEW KLEINWAKS
'ZerosAndTheOne.com
'2009
'CUSTOM FONT LOADED DYNAMICALLY FROM A RESOURCE

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

Module CustomFont

    'PRIVATE FONT COLLECTION TO HOLD THE DYNAMIC FONT
    Private _pfc As PrivateFontCollection = Nothing


    Public ReadOnly Property GetInstance(ByVal Size As Single, _
                                         ByVal style As FontStyle) As Font
        Get
            'IF THIS IS THE FIRST TIME GETTING AN INSTANCE
            'LOAD THE FONT FROM RESOURCES
            If _pfc Is Nothing Then LoadFont()

            'RETURN A NEW FONT OBJECT BASED ON THE SIZE AND STYLE PASSED IN
            Return New Font(_pfc.Families(0), Size, style)

        End Get
    End Property

    Private Sub LoadFont()
        Try
            'INIT THE FONT COLLECTION
            _pfc = New PrivateFontCollection

            'LOAD MEMORY POINTER FOR FONT RESOURCE
            Dim fontMemPointer As IntPtr = _
                Marshal.AllocCoTaskMem( _
                My.Resources.DIGITALDREAMNARROW.Length)

            'COPY THE DATA TO THE MEMORY LOCATION
            Marshal.Copy(My.Resources.DIGITALDREAMNARROW, _
                         0, fontMemPointer, _
                         My.Resources.DIGITALDREAMNARROW.Length)

            'LOAD THE MEMORY FONT INTO THE PRIVATE FONT COLLECTION
            _pfc.AddMemoryFont(fontMemPointer, _
                               My.Resources.DIGITALDREAMNARROW.Length)

            'FREE UNSAFE MEMORY
            Marshal.FreeCoTaskMem(fontMemPointer)
        Catch ex As Exception
            MessageBox.Show(& Err.Number & " " & Err.Description)
        End Try

    End Sub

End Module

正是这个:

   Catch ex As Exception
        MessageBox.Show(& Err.Number & " " & Err.Description)
    End Try

正在显示包含消息的消息框53 File doesn't exists

我真的不知道它为什么会发生,因为它可以在我的计算机上正常工作......我将不胜感激任何帮助尝试!

4

1 回答 1

1

尝试使用此代码

''' <summary>Adds the specified font to the private font collection.</summary>
''' <param name="font">The font to be added.</param>
Public Sub AddFont(ByVal font As Byte())
    If font Is Nothing Then Throw New ArgumentNullException("The font cannot be null.", "font")
    If font.Length = 0 Then Throw New ArgumentException("The length of the font array cannot be 0.", "font")
    Try
        privateFonts.AddMemoryFont(Marshal.UnsafeAddrOfPinnedArrayElement(font, 0), font.Length)
    Catch ex As Exception
        'handle you exceptions here
    End Try
End Sub

并以这种方式将字体添加到集合中

Private Sub LoadFont()
    Try
        'INIT THE FONT COLLECTION
        privateFonts = New PrivateFontCollection
        AddFont(My.Resources.DIGITALDREAMNARROW)
    Catch
    
    '   
    ' the rest of your code
    '
End Sub

假设您将字体资源作为文件添加,它将AddFont作为字节数组传递给方法。

不用说,该AddFont方法假定您有一个已初始化的 PrivateFontCollection 对象,该对象被调用privateFonts,可在该方法的范围内访问。

更新

既然你说我的解决方案不起作用,我在这里上传了一个示例项目。下载并查看如何从资源中加载和使用私有字体。

于 2012-11-30T12:46:13.427 回答