C # install fonts

Source: Internet
Author: User

Traverse all fonts

InstalledFontCollection MyFont = new InstalledFontCollection();FontFamily[] MyFontFamilies = MyFont.Families;List<string> installedFontNames = new List<string>();int Count = MyFontFamilies.Length;for (int i = 0; i < Count; i++){    string FontName = MyFontFamilies[i].Name;    installedFontNames.Add(FontName);}string fontDir = Application.StartupPath + "\\Fonts";if (Directory.Exists(fontDir)){    string[] fontFiles = Directory.GetFiles(fontDir);    foreach (string fontFile in fontFiles)    {        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fontFile);        if (!installedFontNames.Contains(fileNameWithoutExtension))        {        }    }}

 

Font installation

[DllImport("kernel32.dll", SetLastError = true)]static extern int WriteProfileString(string lpszSection, string lpszKeyName, string lpszString);[DllImport("user32.dll")]public static extern int SendMessage(int hWnd, // handle to destination window uint Msg, // message int wParam, // first message parameter int lParam // second message parameter );[DllImport("gdi32")]public static extern int AddFontResource(string lpFileName);private void InstallFont(string fontPath){    string fontFileName = Path.GetFileName(fontPath);    string fontNameWithoutExtenstion = Path.GetFileNameWithoutExtension(fontPath);    string WinFontDir = Environment.GetEnvironmentVariable("WINDIR") + "\\fonts";    string FontPath = WinFontDir + "\\" + fontFileName;    if (!File.Exists(FontPath))    {        File.Copy(fontPath, FontPath);        AddFontResource(FontPath);        WriteProfileString("fonts", fontNameWithoutExtenstion + "(TrueType)", fontFileName);    }}

Temporary font

public static Font CreateFont(string fontName, float height, FontStyle fontStyle = FontStyle.Bold){    Font font;    if (File.Exists(Application.StartupPath + "\\Fonts\\" + fontName + ".ttf"))    {        PrivateFontCollection privateFonts = new PrivateFontCollection();        privateFonts.AddFontFile(Application.StartupPath + "\\Fonts\\" + fontName + ".ttf");        font = new Font(privateFonts.Families[0], height, fontStyle);    }    else    {        font = new Font(fontName, height);    }    return font;}

Memory font

Public static font createfont (setting) {setting. fontfamily = Setting. fontfamily ?? ""; Privatefontcollection p_font = new privatefontcollection (); var o = properties. resources. resourceManager. getObject (setting. fontfamily); If (O is byte []) {byte [] B _font = O as byte []; intptr meadd = marshal. allochglobal (marshal. sizeof (typeof (byte) * B _font.length); marshal. copy (B _font, 0, meadd, B _font.length); p_font.addmemoryfont (meadd, B _font.length); return new font (p_font.families [0], 14.25f, fontstyle. bold);} return new font ("", 14.25f, fontstyle. bold );}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.