locked
How To Read An Embedded Font File In Visual Basic RRS feed

  • Question

  • I am working with the following code and get an exception error  (Unable to Load Font dungeon.ttf)

    I currently have the font file saved in a Resource and it's Properties Set To Embedded


    Imports System.IO

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

    Module Embedded_Font
        Public Function LoadFont(Asm As Assembly, Name As String, Size As Integer, Style As FontStyle) As Font
            Using Collection As New PrivateFontCollection
                Dim Bytes() As Byte = Embedded_Font.FontData(Asm, Name)
                Dim Ptr As IntPtr = Marshal.AllocCoTaskMem(Bytes.Length)
                Marshal.Copy(Bytes, 0, Ptr, Bytes.Length)
                Collection.AddMemoryFont(Ptr, Bytes.Length)
                Marshal.FreeCoTaskMem(Ptr)
                Return New Font(Collection.Families(0), Size, Style)
            End Using
        End Function
        Private Function FontData(Asm As Assembly, Name As String) As Byte()
            Using Stream As Stream = Asm.GetManifestResourceStream(Name)
                If (Stream Is Nothing) Then Throw New Exception(String.Format("Unable to load font '{0}'", Name))
                Dim Buffer() As Byte = New Byte(CInt(Stream.Length - 1)) {}
                Stream.Read(Buffer, 0, CInt(Stream.Length))
                Return Buffer
            End Using
        End Function
    End Module

    Usage:

    MyControl.Font = Embedded_Font.LoadFont(Me.GetType.Assembly, "dungeon.ttf", 9, FontStyle.Regular)

    Any Recommendations to solve this problem

     

    Sunday, July 12, 2020 5:11 PM

All replies

  • Check if the next line works:

       Dim Bytes() As Byte = My.Resources.dungeon_ttf

    Then you can remove FontData, adjust and simplify the code.

    Sunday, July 12, 2020 6:51 PM
  • I didn't include the Project Namespace With The Font Name, I was hoping that this would

    work with TextBox and RichTextBox, but so far labels work

    Saturday, July 18, 2020 3:25 PM
  • Hi David William Smith,

    Did you resolve the exception and successfully read an embedded font file? 

    >>I was hoping that this would work with TextBox and RichTextBox, but so far labels work

    Could you provide more details about this sentence?

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 20, 2020 7:39 AM
  • I tested it with a Rich Text Box and a Text Box and a Label.

    It works fine with all of them

    David William Smith

    Monday, August 10, 2020 3:13 PM
  • Hi,

    >>It works fine with all of them

    Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Besides, if you need further assistance, please let me know.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 11, 2020 6:31 AM