{"id":4705,"date":"2021-03-17T10:28:12","date_gmt":"2021-03-17T09:28:12","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=4705"},"modified":"2022-07-25T08:31:51","modified_gmt":"2022-07-25T07:31:51","slug":"c-use-load-custom-fonts","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-use-load-custom-fonts\/","title":{"rendered":"C# Use\/Load custom fonts (WinForms +)"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System.IO;\nusing System.Reflection;\nusing System.Collections.Generic;\nusing System.Drawing;\nusing System.Drawing.Text;\n\npublic static class FontConfig\n{\n\tprivate static PrivateFontCollection Collection;\n\n\tstatic FontConfig()\n\t{\n\t\tLoadFonts(new List&lt;string>() {\n\t\t\t@\"Quicksand-VariableFont_wght.ttf\",\n\t\t\t@\"Verdana.ttf\"\n\t\t});\n\t}\n\n\tprivate static void LoadFonts(IEnumerable&lt;string> fontFiles)\n\t{\n\t\tCollection = new PrivateFontCollection();\n\t\tforeach (var fontFile in fontFiles)\n\t\t{\n\t\t\tvar fileName = GetRelativePath(fontFile);\n\t\t\tif (!File.Exists(fileName)) continue;\n\t\t\tCollection.AddFontFile(fileName);\n\t\t}\n\t}\n\n\tprivate static string GetRelativePath(string fileName) => Path.Combine(GetExePath(), fileName);\n\tprivate static string GetExePath() => Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);\n\n\tpublic static Font GetFont(string name, float size, FontStyle fontStyle = FontStyle.Regular)\n\t{\n\t\tvar family = new FontFamily(name, Collection);\n\t\treturn new Font(family, size, fontStyle, GraphicsUnit.Pixel);\n\t}\n\t\/\/public static Font ControlFont { get; } = GetFont(\"Quicksand\", 12);\n\tpublic static Font ControlFont { get; } = GetFont(\"Verdana\", 12);\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-4705","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=4705"}],"version-history":[{"count":3,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4705\/revisions"}],"predecessor-version":[{"id":4708,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4705\/revisions\/4708"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=4705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=4705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=4705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}