Results 1 to 4 of 4

Thread: Embedding custom fonts into your application

  1. #1

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    57

    Red face Embedding custom fonts into your application

    Theire are several ways to make use of a font type that is not standard on a platform your application is executed from, i tried most of them and ive come to learn that the following method was best for my desires.


    First we add and embed our ttf (font) file in the application
    (in this case im using the Fixedsys Excelsior 3.01)

    Name:  Ll8mdhB.jpg
Views: 2586
Size:  12.9 KB

    Make sure it is embedded into your application upon build

    Name:  P2iL93p.png
Views: 2466
Size:  6.5 KB


    You can copy the following module in your application.

    Code:
    Imports System.IO
    Imports System.Reflection
    Imports System.Drawing.Text
    Imports System.Runtime.InteropServices
    
    Module Helpers
        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 = Helpers.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

    To initialize the font into a control for let say textbox or richttextbox, use the following statement

    Code:
    MyControl.Font = Helpers.LoadFont(Me.GetType.Assembly, "Sirc.FSEX300.ttf", 9, FontStyle.Regular)
    Notice that it does require the application namespace to locate the font data in the current assembly,
    without it, it will not work.


    I hope it serves you well as it did for me.

    - Barret

  2. #2
    New Member
    Join Date
    Jul 2020
    Posts
    2

    Re: Embedding custom fonts into your application

    When Testing This Code I Get An Exception Error Unable To Load Font

    Any Suggestions

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,394

    Re: Embedding custom fonts into your application

    Quote Originally Posted by David William Smith View Post
    When Testing This Code I Get An Exception Error Unable To Load Font

    Any Suggestions

    we cant pull a magic wand out. Post your code. Not mind readers.

    https://docs.microsoft.com/en-us/dot...System_String_

  4. #4
    New Member
    Join Date
    Jul 2020
    Posts
    2

    Re: Embedding custom fonts into your application

    Quote Originally Posted by ident View Post
    we cant pull a magic wand out. Post your code. Not mind readers.

    https://docs.microsoft.com/en-us/dot...System_String_
    The Web Page Was Very Helpful, I Wasn't Using The Applications Namespace In The File String.

    Thanks.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width