【问题标题】:Couldnt use a custom font - "file not found"无法使用自定义字体 - “找不到文件”
【发布时间】:2016-07-12 16:10:25
【问题描述】:

我在关注这个: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 消息。

我真的不知道为什么会这样,因为它可以在我的计算机上运行而没有任何问题...如果有任何帮助尝试,我将不胜感激!

【问题讨论】:

  • 删除 FreeCoTaskMem 调用。

标签: vb.net fonts privatefontcollection


【解决方案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。下载并查看如何从资源中加载和使用私有字体。

【讨论】:

  • 我应该用AddFont(My.Resources.DIGITALDREAMNARROW) 替换_pfc.AddMemoryFont(fontMemPointer, _ My.Resources.DIGITALDREAMNARROW.Length) 行吗?
  • 是的,将_pfc = New PrivateFontCollection 之后的所有内容替换为AddFont(My.Resources.DIGITALDREAMNARROW)。 (查看我的编辑
  • 还是一样。除了现在,异常来自AddFont 函数。这是我的全部代码:pastebin.com/fhFx3pqz.
  • 当我试图打开PrivateFonts.vbproj 时,什么也没有发生。
  • 您使用的是哪个版本的 Visual Studio?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 2017-10-10
  • 2017-05-31
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多