Search code examples
c#vb.netfonts

How to load a font from a file in VB and C#?


I want to load a font in my VB Form program from a file.

e.g.: My font is in the same folder of my .exe program, and I want it to stay an external resource (that we can replace so that it change the entire program's font).


Solution

  • Here's and example of how you can do it in C#:

    System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();
    privateFonts.AddFontFile("C:\\Documents and Settings\\somefont.ttf");
    System.Drawing.Font font = new Font(privateFonts.Families[0], 12);
    label1.Font = font;
    

    Or, in VB.NET:

    Dim privateFonts As New System.Drawing.Text.PrivateFontCollection()
    privateFonts.AddFontFile("C:\Documents and Settings\somefont.ttf")
    Dim font As New System.Drawing.Font(privateFonts.Families(0), 12)
    label1.Font = font
    

    Look THIS for more details.