1using System; 2using System.Drawing; 3using System.Drawing.Text; 4using System.IO; 5using System.Runtime.InteropServices; 6 7namespace WindowCapture.Helpers 8{ 9 public static class FontHelper 10 { 11 private static PrivateFontCollection _fonts; 12 private static FontFamily _bebasFont; 13 14 public static FontFamily BebasFont { get { EnsureLoaded(); return _bebasFont; } } 15 16 public static void EnsureLoaded() 17 { 18 if (_fonts != null) return; 19 try 20 { 21 _fonts = new PrivateFontCollection(); 22 var assembly = typeof(FontHelper).Assembly; 23 string resName = "WindowCapture.Bebas_Neue_Bold.ttf"; 24 foreach (string name in assembly.GetManifestResourceNames()) 25 { 26 if (name.EndsWith("Bebas Neue Bold.ttf", StringComparison.OrdinalIgnoreCase)) 27 { resName = name; break; } 28 } 29 using (Stream s = assembly.GetManifestResourceStream(resName)) 30 { 31 if (s != null) 32 { 33 byte[] d = new byte[s.Length]; s.Read(d, 0, (int)s.Length); 34 IntPtr ptr = Marshal.AllocCoTaskMem(d.Length); 35 Marshal.Copy(d, 0, ptr, d.Length); 36 _fonts.AddMemoryFont(ptr, d.Length); 37 Marshal.FreeCoTaskMem(ptr); 38 if (_fonts.Families.Length > 0) _bebasFont = _fonts.Families[0]; 39 } 40 } 41 } 42 catch { } 43 } 44 45 public static Font CreateFont(float size, FontStyle style = FontStyle.Regular) 46 { 47 EnsureLoaded(); 48 if (_bebasFont != null) 49 return new Font(_bebasFont, size, style, GraphicsUnit.Pixel); 50 return new Font("Arial", size, style, GraphicsUnit.Pixel); 51 } 52 } 53}