[英]Drawing text using font from file not working
I'm trying to load a private font, using System.Drawing.Text.PrivateFontCollection. 我正在尝试使用System.Drawing.Text.PrivateFontCollection加载私有字体。 The goal is not to have to install the font on the system.
目标不是必须在系统上安装字体。
All the examples i find it look pretty simple. 所有的例子我发现它看起来很简单。 Just load using PrivateFontCollection and then create a font from it.
只需使用PrivateFontCollection加载,然后从中创建一个字体。
Below my simple class to test it. 在我的简单类下面测试它。
It works only if i install the font. 它只有在我安装字体时才有效。 In not, the text is printed in the dialog preview as using some default font.
否则,文本将在对话框预览中打印为使用某些默认字体。 I checked that the font is correctly loaded.
我检查了字体是否正确加载。 What i'm missing ?
我错过了什么? Thank for any help.
感谢您的帮助。
public partial class Test : Form
{
private PrintDocument printDocument1 = new PrintDocument();
System.Drawing.Text.PrivateFontCollection privateFonts;
private Font _barCodeFont;
public Test()
{
InitializeComponent();
}
private void Test_Load(object sender, EventArgs e)
{
privateFonts = new System.Drawing.Text.PrivateFontCollection();
privateFonts.AddFontFile("Code128.ttf");
}
private void btbTest_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.DocumentName = "Label";
PrintPreviewDialog pp = new PrintPreviewDialog();
pp.Document = pd;
pp.WindowState = FormWindowState.Normal;
pp.ShowDialog();
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
_barCodeFont = new Font(privateFonts.Families[0], 12, FontStyle.Regular);
ev.Graphics.DrawString("Should Be a bar code", _barCodeFont, Brushes.Black, 0, 0);
ev.HasMorePages = false;
}
}
try this 试试这个
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Font _barCodeFont = new Font(privateFonts.Families[0], 12, FontStyle.Regular);
ev.Graphics.DrawString("Should Be a bar code", _barCodeFont, Brushes.Black, 0, 0);
ev.HasMorePages = false;
}
and remove 并删除
private Font _barCodeFont; private Font _barCodeFont;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.