0

I have been trying (without success) to use and embedded font in my VB project. I have done it before but now I can't remember how to do it. I have been searching and searching on here and other websites, but stuff I have tried isn't working.

This is the only way I can do it at the moment. It works but I want to embed the font.

Dim privateFonts As New System.Drawing.Text.PrivateFontCollection()
    privateFonts.AddFontFile("D:\somefont.ttf")
    Dim font As New System.Drawing.Font(privateFonts.Families(0), 12)
    Label1.Font = font

I have embedded the font no problem.

What code do I need to pull the font from the resource file and use it, on a label or panel, for example.

I know this has been asked so many times before, but nothing I try is working.

Thanks (with some embarrassment) in advance. Neil.

cardshark
  • 21
  • 4
  • I contradicted myself I think. I have embedded the font, I just need the code to access the font in the project. – cardshark Jan 14 '22 at 16:34
  • Does this answer your question? [How to embed fonts for Use in a Visual Basic Project?](https://stackoverflow.com/questions/9589932/how-to-embed-fonts-for-use-in-a-visual-basic-project) – Andrew Morton Jan 14 '22 at 16:51
  • @AndrewMorton - this is what I tried... my problem is the line of code that ACTUALLY assigns the font to a control eg. Label1.Font=... that's what I can't remember. – cardshark Jan 14 '22 at 17:49

1 Answers1

0

I found a working solution on another site.

Imports System.Runtime.InteropServices
Public Class Form1
Public privateFontCollection As New Drawing.Text.PrivateFontCollection
Public FamilyName As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim data As System.IntPtr = Marshal.AllocCoTaskMem(My.Resources.amstrad_cpc464.Length - 1)
    Marshal.Copy(My.Resources.amstrad_cpc464, 0, data, My.Resources.amstrad_cpc464.Length)
    privateFontCollection.AddMemoryFont(data, My.Resources.amstrad_cpc464.Length)
    Marshal.FreeCoTaskMem(data)
    Dim fnt As Font = New Font(privateFontCollection.Families(0), 15)
    Label1.Font = fnt
End Sub
End Class
cardshark
  • 21
  • 4