【发布时间】:2019-08-23 10:00:14
【问题描述】:
我一直在尝试在我的 WinForm 应用程序上获取自定义字体。但是,似乎无论我做什么,字体都无法正确呈现。它是.ttf 并且确实显示在我的标签上,但不是很好。
我关注了这个StackOverflow answer,但这是我得到的结果:
底部“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# winforms fonts rendering