-
Jan 11th, 2022, 02:05 PM
#1
Thread Starter
Member
[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
Where can I find this option or is it required in VS 2017?
-
Jan 11th, 2022, 06:33 PM
#2
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.
-
Jan 12th, 2022, 01:40 PM
#3
Thread Starter
Member
Re: Embedding font
Originally Posted by Poppa Mintin
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)
-
Jan 12th, 2022, 01:56 PM
#4
Thread Starter
Member
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
-
Jan 12th, 2022, 07:06 PM
#5
Re: [RESOLVED] Embedding font
Originally Posted by 6h057
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.
-
Jan 13th, 2022, 01:26 PM
#6
Thread Starter
Member
Re: [RESOLVED] Embedding font
-
Jan 13th, 2022, 04:53 PM
#7
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.
-
Jan 20th, 2022, 03:04 PM
#8
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|