Results 1 to 8 of 8

Thread: [RESOLVED] Embedding font

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    34

    Resolved [RESOLVED] Embedding font

    Hi all.
    Im trying to embedd font in my application. Trying a few codes and all not working. For example below code from here https://www.vbforums.com/showthread....06#post5551506

    Code:
    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
    Code:
    Label1.Font = Helpers.LoadFont(Me.GetType.Assembly, "hydrogen", 24, FontStyle.Regular)

    Only size and style is changing. I missed only this tip "Make sure it is embedded into your application upon build". In VS2017 I didn't see this field Name:  Clipboard01.jpg
Views: 364
Size:  22.8 KB
    Where can I find this option or is it required in VS 2017?

  2. #2
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,428

    Re: Embedding font

    Hi 6h057,

    You might find what you need HERE

    I hope it helps.

    Poppa
    Along with the sunshine there has to be a little rain sometime.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    34

    Re: Embedding font

    Quote Originally Posted by Poppa Mintin View Post
    Hi 6h057,

    You might find what you need HERE

    I hope it helps.

    Poppa

    Thank you Poppa Mintin. Indeed this propertie solve the problem. Why nobody put this important thing!!!
    Set the ‘UseCompatibleTextRendering property ‘, to true (the default is false)

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    34

    Re: [RESOLVED] Embedding font

    Hmm, this parameter couse this, during resizing the form. Any ide how to fix this?
    Attachment 183570 INTO THIS Attachment 183571

  5. #5
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,428

    Re: [RESOLVED] Embedding font

    Quote Originally Posted by 6h057 View Post
    Hmm, this parameter couse this, during resizing the form. Any ide how to fix this?
    Attachment 183570 INTO THIS Attachment 183571
    Sadly these attachments don't go anywhere.

    I suggest that you try again, but start a new thread.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    34

    Re: [RESOLVED] Embedding font


  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,740

    Re: [RESOLVED] Embedding font

    If you're trying to attach an image, use a free image server (like https://imgbb.com/) or go to advance reply, click on attachments, and upload the image, and then insert the attachment.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    34

    Re: [RESOLVED] Embedding font

    Ok, I solved as below.

    Code:
    Public Collection As PrivateFontCollection
    
     Label1.UseCompatibleTextRendering = True
    
            Collection = New PrivateFontCollection
            Dim bytes() As Byte = My.Resources.hydrogen
            Dim Ptr As IntPtr = Marshal.AllocCoTaskMem(bytes.Length)
            Marshal.Copy(bytes, 0, Ptr, bytes.Length)
            Collection.AddMemoryFont(Ptr, bytes.Length)
            Marshal.FreeCoTaskMem(Ptr)
    
            Label1.Font = New Font(Collection.Families(0), 24, FontStyle.Regular)

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