Search code examples
c#winformsfontsrendering

Ugly font rendering


I have been trying to get a custom font on my WinForm application. However, it seems whatever I do, the font won't render properly. It is a .ttf and does display on my label, just not very well.

I followed this StackOverflow answer but this is the result I am getting:

The two labels at the bottom '0%' & '2.6MB' are both 'Courier New'. I will switch them over once I have got my top label rendering correctly.

Ugly font render

My code is near identical to the aforementioned answer (all of the bellow runs in FormLoad):

// Create a private font collection object.
PrivateFontCollection pfc = new PrivateFontCollection();

// Select the font from 'Resources'.
// My font here is "Volter__28Goldfish_29.ttf".
int fontLength = Properties.Resources.Volter__28Goldfish_29.Length;

// Create a buffer to read in to.
byte[] fontdata = Properties.Resources.Volter__28Goldfish_29;

// Create an unsafe memory block for the font data.
IntPtr data = Marshal.AllocCoTaskMem(fontLength);

// Copy the bytes to the unsafe memory block.
Marshal.Copy(fontdata, 0, data, fontLength);

// Pass the font to the font collection.
pfc.AddMemoryFont(data, fontLength);

// Set custom font.
lblUpdate.Font = new Font(pfc.Families[0], 8);

Solution

  • I thought it would be too obvious to be true... my issue was the fact that my font size was either 6F or 8F as, I didn't think an odd number would be the correct size. All I had to do was change it to 7F.

    So I will keep this post up as a word of warning, if your pixel font looks ugly. Double check you've put the correct font size in.

    (Note: Your font may also appear somewhat distorted if you haven't set the property UseCompatibleTextRendering to True)