How can I embed font in Visual Basic .Net application? which should valid at every operating system.
You can't. There is no cross-platform standard at this point for embedded fonts in a standard Winforms application. The best you can do is embed individual, OS-specific font files, detect which OS you're on, and then install the font programmatically.
On the other hand, A WPF application in VB.net might work for your needs, but I have a feeling this isn't where you're going. For information on how to package fonts with a WPF application, see this MSDN article.
It is possible to embed a font in an application and use it if that font is not available on the user's system.
You simply create a PrivateFontCollection and populate it with your fonts then you can use them as you please. According to MSDN, this method does not apply to operating systems before Windows 2000.
From Remarks section of PrivateFontCollection.AddFontFile method:
When using a private font on operating systems before Windows 2000, the default font, typically Microsoft Sans Serif, will be substituted.
If you intend your application to be used on Windows 2000 and newer, you can follow this code I wrote to see how to implement private fonts.
Public Class Form1
Dim pfc As System.Drawing.Text.PrivateFontCollection
Dim ifc As System.Drawing.Text.InstalledFontCollection
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
pfc = New System.Drawing.Text.PrivateFontCollection()
ifc = New System.Drawing.Text.InstalledFontCollection()
LoadPrivateFonts({My.Resources.Series_60_ZDigi, My.Resources.Times_NR_Phonetics_2})
End Sub
''' <summary>Loads the private fonts.</summary>
''' <param name="fonts">The fonts to be loaded into the private font collection.</param>
Private Sub LoadPrivateFonts(ByVal fonts As IEnumerable(Of Byte()))
For Each resFont In fonts
pfc.AddMemoryFont(Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(resFont, 0), resFont.Length)
Next
End Sub
''' <summary>Gets the FontFamily whose name matches the one specified.</summary>
''' <param name="fontName">Name of the FontFamily to be returned.</param>
''' <param name="defaultFamily">
''' Optional. The default font family to be returned if the specified font is not found
''' </param>
Private Function GetFontFamily(ByVal fontName As String, Optional ByVal defaultFamily As FontFamily = Nothing) As FontFamily
If String.IsNullOrEmpty(fontName) Then
Throw New ArgumentNullException("fontName", "The name of the font cannont be null.")
End If
Dim foundFonts = From font In ifc.Families.Union(pfc.Families) Where font.Name.ToLower() = fontName.ToLower()
If foundFonts.Any() Then
Return foundFonts.First()
Else
Return If(defaultFamily, FontFamily.GenericSansSerif)
End If
End Function
Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
'free the resources used by the font collections
pfc.Dispose()
ifc.Dispose()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g = e.Graphics
Using br As Brush = Brushes.Black
g.DrawString("1234567890ABCDEF", New Font(GetFontFamily("Series 60 ZDigi"), 18), br, New Point(20, 20))
g.DrawString("ABCDEFGHIJKLMNOP", New Font(GetFontFamily("Times NR Phonetics 2"), 18), br, New Point(20, 100))
End Using
End Sub
End Class
I load the two fonts I use in the application (Series 60 ZDigi, a font from my Nokia phone, and Times NR Phonetics 2, a font from my dictionary application) from the resources into the private font collection in the Sub New()
.
I then call the GetFontFamily
method to get the desired font to paint onto the form.
It shouldn't be too hard to incorporate this into your applications.
Cheers.