none
Custom Font RRS feed

  • Question

  • Currently I have a font in a label using "Monotype Corsiva, 18pt, style=Italic" If a user does not have this font, how do I embed my font into my app so regardless of rather or not they have the font, my font will display correctly for everyone.
    Monday, September 25, 2017 7:19 PM

All replies

  • You can embed it into resources.

    For example : Embedding Font To Resources

    Monday, September 25, 2017 8:09 PM
  • Hello old_School,

    As Castorix31 said , you should embed the font as resource . I'm not sure which program you are using . The following is steps for example in winform.

    1.Double-click Resources.resx >> choose Resource tab >>click Add Resource/Add Existing File and select your .ttf file.(the system ttf file usually under C:\Windows\Fonts).

    2.right-click .ttf file (now in a Resources folder) and go to Properties. Set the Build Action to "Embedded Resource".

    3.Add the code as below.

    public partial class Form2 : Form
        {
            [System.Runtime.InteropServices.DllImport("gdi32.dll")]
            private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
                IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);
    
            private PrivateFontCollection fonts = new PrivateFontCollection();
    
            Font myFont;
    
            public Form2()
            {
                InitializeComponent();
    
                byte[] fontData = Properties.Resources.MTCORSVA;
                IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
                System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
                uint dummy = 0;
                fonts.AddMemoryFont(fontPtr, Properties.Resources.MTCORSVA.Length);
                AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.MTCORSVA.Length, IntPtr.Zero, ref dummy);
                System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
                // label using "Monotype Corsiva, 18pt, style=Italic"
                myFont = new Font(fonts.Families[0],18.0F,FontStyle.Italic);
    
            }

    And use your font.

    private void Form1_Load(object sender, EventArgs e)
    {
        label1.Font = myFont;
    }

    The result as below.

    Sincerely,

    feih_7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 26, 2017 3:05 AM
    Moderator
  • Hello old_school,

    Is there anything updated?

    And if the issue has been solved ,you could mark the reply as answer  , This can be beneficial to other community members reading this thread.

    Best regards,

    neil hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 27, 2017 7:03 AM
    Moderator