I'm trying to make an aplication with custom fonts on some labels and I dont want the users having to install the fonts in order to see them. So I want to embed the font into the aplication so it can use it from inside itself.
I already have the fonts I'm using as an embedded resource. I just dont know how to use them from there.
I found this code, I'm trying to use it but I get an error when I run it:
- Code: Select all
Public Class Form1
Dim pfc As New System.Drawing.Text.PrivateFontCollection()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'load the resource
Dim fontStream As System.IO.Stream = Me.GetType().Assembly.GetManifestResourceStream("embedfont.vb.Alphd___.ttf")
'create an unsafe memory block for the data
Dim data As System.IntPtr = Runtime.InteropServices.Marshal.AllocCoTaskMem(fontStream.Length)
'create a buffer to read in to
Dim fontdata() As Byte
ReDim fontdata(fontStream.Length)
'fetch the font program from the resource
fontStream.Read(fontdata, 0, fontStream.Length)
'copy the bytes to the unsafe memory block
Runtime.InteropServices.Marshal.Copy(fontdata, 0, data, fontStream.Length)
'pass the font to the font collection
pfc.AddMemoryFont(data, fontStream.Length)
'close the resource stream
fontStream.Close()
'free the unsafe memory
Runtime.InteropServices.Marshal.FreeCoTaskMem(data)
End Sub
End Class
On the line:
- Code: Select all
Dim data As System.IntPtr = Runtime.InteropServices.Marshal.AllocCoTaskMem(fontStream.Length)
It says: "Object reference not set to an instance of an object." when I run it
If anyone could tell me how to make this work I would be very happy, I've been tryng to make this work since yesterday and my head hurts.


