addFontFile from Resources

c#, visual-studio, winforms

Solution

If you included your font in the resources

Try this function

private void AddFontFromMemory()
{
    Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("yourfont.ttf");
 
    byte[] fontdata = new byte[fontStream.Length];
    fontStream.Read(fontdata,0,(int)fontStream.Length);
    fontStream.Close();

    unsafe
    {
        fixed(byte * pFontData = fontdata)
        {
            pfc.AddMemoryFont((System.IntPtr)pFontData,fontdata.Length);
        }
    }
}

Edited

How load resource from assembly:(YourNamespace.file.ttf)

Stream fontStream = Assembly.GetExecutingAssembly()
 .GetManifestResourceStream("WindowsFormsApplication1.SBADR.TTF");

My solution explorer:

Problem

I have added a custom font using below code: ``` PrivateFontCollection pfc = new PrivateFontCollection(); pfc.AddFontFile("C:\\Path To\\YourFont.ttf"); label1.Font = new System.Drawing.Font(pfc.Families[0], 16, FontStyle.Regular); ``` I added the font file in resources. How do I add with `addFontFile` from resources?

Original source

Related problems