无法使用自定义字体 - “找不到文件”

时间:2012-11-30 08:19:33

标签: vb.net fonts privatefontcollection

我遵循这个: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消息的消息框。

我真的不知道它为什么会发生,因为它可以在我的电脑上运行而没有任何问题...我会感激任何帮助尝试!

1 个答案:

答案 0 :(得分: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方法假设您有一个名为privateFonts的初始化PrivateFontCollection对象,该对象可在该方法的范围内访问。

更新

由于您说我的解决方案无效,我已上传了示例项目here。下载并查看如何从资源加载和使用私有字体。