简体   繁体   English

使用文件中的字体绘制文本不起作用

[英]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.

相关问题 使用 System.Drawing.Font.FromHfont(IntPtr hfont) 从 SYSTEM_FONT Stock 对象创建 System.Drawing.Font - System.Drawing.Font Creation From SYSTEM_FONT Stock Object using System.Drawing.Font.FromHfont(IntPtr hfont) 需要在c#中从带有框架字体的文本中获取文本轮廓或文本绘制形状 - Need to get text outlines or text drawing shapes from text with framework font in c# System.Drawing.Font 未按预期工作 - System.Drawing.Font not working as expected 从模板文件创建PDF,绘制文本并打印文件 - Creating PDF from template file, drawing text, and printing the file 使用GDI +绘制文本时是否可以定义精确的字体大小? - Is it possible to define a precise font size when drawing text using GDI+? 我想使用C#.net从Excel文件中获取其字体样式和权重的文本吗? - I want to get text from an excel file with its font style and weight using C# .net? 使用System.Drawing.Printing将文件发送到打印无法正常工作 - Sending file to print not working using System.Drawing.Printing 使用System.Drawing.Font限制字体大小? - Limit font size using System.Drawing.Font? 从文本文件读取不起作用 - Reading from a text file not working 使用DrawString()正确绘制文本 - Drawing text correctly using DrawString()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM