首页 > 解决方案 > 丑陋的字体渲染

问题描述

我一直在尝试在我的 WinForm 应用程序上获取自定义字体。但是,似乎无论我做什么,字体都不会正确呈现。这是一个.ttf并且确实显示在我的标签上,只是不是很好。

我遵循了这个StackOverflow 答案,但这是我得到的结果:

底部“0%”和“2.6MB”的两个标签都是“Courier New”。正确渲染顶部标签后,我将切换它们。

丑陋的字体渲染

我的代码与上述答案几乎相同(所有波纹管都在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);

标签: c#winformsfontsrendering

解决方案


我认为这太明显了……我的问题是我的字体大小是6F或者8F是,我认为奇数不是正确的大小。我所要做的就是将其更改为7F.

因此,如果您的像素字体看起来很难看,我会保留这篇文章作为警告。仔细检查您是否输入了正确的字体大小。

注意:如果您没有将属性设置为 ,您的字体也可能会显得有些UseCompatibleTextRendering失真True


推荐阅读