Hi,
i'm writing a .Net application for printing barcodes to my climbing club.
For simplicity reasons, i've embedded my barcode font as a ressource file.
When loading my font as a private font i am able to use it in labels,
textboxes, etc... but not in printing controls, like printpreviewdialog
Could you please help me ?
Here is my code for loading my font :
Imports System
Dim MyEmbdFonts() As String = {"code25I.ttf"}
Dim FntFC As Drawing.Text.PrivateFontCollection = GetFont(MyEmbdFonts)
Public CBFont As New Font(FntFC.Families(0), 42)
Public CFont As New Font("Arial", 10)
Private Declare Auto Function AddFontMemResourceEx Lib "Gdi32.dll" _
(ByVal pbFont As IntPtr, ByVal cbFont As Integer, _
ByVal pdv As Integer, ByRef pcFonts As Integer) As IntPtr
Public Function GetFont(ByVal FontResource() As String) As _
Drawing.Text.PrivateFontCollection
'Get the namespace of the application
Dim NameSpc As String = _
Reflection.Assembly.GetExecutingAssembly().GetName ().Name.ToString()
Dim FntStrm As IO.Stream
Dim FntFC As New Drawing.Text.PrivateFontCollection()
Dim i As Integer
For i = 0 To FontResource.GetUpperBound(0)
'Get the resource stream area where the font is located
FntStrm = _
Reflection.Assembly.GetExecutingAssembly().GetMani festResourceStream(
_
NameSpc + "." + FontResource(i))
'Load the font off the stream into a byte array
Dim ByteStrm(CType(FntStrm.Length, Integer)) As Byte
FntStrm.Read(ByteStrm, 0, Int(CType(FntStrm.Length, Integer)))
'Allocate some memory on the global heap
Dim FntPtr As IntPtr = _
Runtime.InteropServices.Marshal.AllocHGlobal( _
Runtime.InteropServices.Marshal.SizeOf(GetType(Byt e)) * _
ByteStrm.Length)
'Copy the byte array holding the font into the allocated memory.
Runtime.InteropServices.Marshal.Copy(ByteStrm, 0, _
FntPtr, ByteStrm.Length)
'Add the font to the PrivateFontCollection
FntFC.AddMemoryFont(FntPtr, ByteStrm.Length)
Dim pcFonts As Int32
pcFonts = 1
AddFontMemResourceEx(FntPtr, ByteStrm.Length, 0, pcFonts)
'Free the memory
Runtime.InteropServices.Marshal.FreeHGlobal(FntPtr )
Next
Return FntFC
End Function
Thanks,
Davy.