C# Embedded font for button

  • Thread starter garethrichardadams
  • Start date
G

garethrichardadams

Hello all,

I've added a font to my project and set it to "Embedded Resource".

I load the font into a global PrivateFontCollection. (InitCustomFont -
shown below)

I then set the font of a label to be equal to the custom font
(SetCustomFont - shown below)

The font size changes but the family doesn't. If I write directly to
the form using Graphics.DrawString the new font is displayed???

Does that mean I can't change the font of labels/buttons etc. to
resource fonts?

Gareth

private void InitCustomFont()
{
// Load the resource
Stream fontStream =
this.GetType().Assembly.GetManifestResourceStream("Graphyx.custom.ttf");

// Create a buffer to read into
byte[] fontData = new byte[fontStream.Length];

// Fetch the font program from the resource
fontStream.Read(fontData, 0, (int)fontStream.Length);

unsafe
{
fixed (byte* pFontData = fontData)
{

// Pass the font to the font collection
pfc.AddMemoryFont((System.IntPtr)pFontData,
(int)fontStream.Length);
}
}
// Close the resource stream
fontStream.Close();
}

private void SetCustomFont()
{
System.Drawing.Font fn;
FontFamily ff;

ff = pfc.Families[0];

fn = new Font(ff, 16, FontStyle.Regular);

//lblHelp.Font = new System.Drawing.Font("Courier New", 16.0f,
(FontStyle)2);
lblHelp.Font = fn;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top