How can I bundle a font with my .net winforms application?

后端 未结 3 1482
不思量自难忘°
不思量自难忘° 2021-01-21 01:40

I\'d like to use a non-standard font for my .net 3.0 Winforms application.

This font might be installed on some of my user\'s computer, but it will clearly be missing on

相关标签:
3条回答
  • 2021-01-21 02:01

    This page explains in detail how to embed a font into a winforms project.

    0 讨论(0)
  • 2021-01-21 02:04

    You'll have to use an installer to get the font registered on the target machine. But maybe you won't have to, GDI+ supports private fonts.

    0 讨论(0)
  • 2021-01-21 02:12

    Here's a blog article I wrote that shows a way to embed fonts as resources in your application (no dll imports necessary :).

    Embedding Fonts in your .Net Application

    Here's the class I create where all the magic happens. The blog article includes instructions and an example of using it.

    using System.Drawing;
    using System.Drawing.Text;
    using System.Runtime.InteropServices;
    namespace EmbeddedFontsExample.Fonts
    {
        public class ResFonts
        {
            private static PrivateFontCollection sFonts;
            static ResFonts()
            {
                sFonts = new PrivateFontCollection();
                // The order the fonts are added to the collection 
                // should be the same as the order they are added
                // to the ResFontFamily enum.
                AddFont(MyFonts.Consolas);
            }
            private static void AddFont(byte[] font)
            {
                var buffer = Marshal.AllocCoTaskMem(font.Length);
                Marshal.Copy(font, 0, buffer, font.Length);
                sFonts.AddMemoryFont(buffer, font.Length);
            }
            public static Font Create(
                ResFontFamily family, 
                float emSize, 
                FontStyle style = FontStyle.Regular, 
                GraphicsUnit unit = GraphicsUnit.Pixel)
            {
                var fam = sFonts.Families[(int)family];
                return new Font(fam, emSize, style, unit);
            }
        }
        public enum ResFontFamily
        {
            /// <summary>Consolas</summary>
            Consolas = 0
        }
    }
    
    0 讨论(0)
提交回复
热议问题