gpt4 book ai didi

c# - PrintPreviewDialog中MeasureString测得的字符串高度与实际打印时不一致

转载 作者:行者123 更新时间:2023-11-30 18:11:04 24 4
gpt4 key购买 nike

我正在尝试测量一些文本的高度以用于表格打印目的。

这是代码。在我的例子中,它在预览和实际页面上打印不同的数字。我现在无法尝试使用 Microsoft Office Document Image Writer 以外的任何打印机,但我很确定这不是打印机问题。

也许有人找到了解决这个问题的方法?

    private void button1_Click(object sender, EventArgs e)
{
Print();
}

public void Print()
{
PrintDocument my_doc = new PrintDocument();
my_doc.PrintPage += new PrintPageEventHandler(this.PrintPage);

PrintPreviewDialog my_preview = new PrintPreviewDialog();
my_preview.Document = my_doc;

my_preview.ShowDialog();

my_doc.Dispose();
my_preview.Dispose();
}

private void PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Pixel;

string s = "String height is ";

SizeF h = e.Graphics.MeasureString(s, new Font("Arial", 24));

e.Graphics.DrawString(s + Convert.ToString(h.Height),
new Font("Arial", 24), new SolidBrush(Color.Black), 1, 1);
}

最佳答案

我猜问题出在 System.Drawing.Graphics 基于 GDI+,而实际打印是基于 GDI。

您可以替换对 MeasureString 的调用以使用基于 GDI 的方法:

SizeF hT = TextRenderer.MeasureText(s, new Font("Arial", 24));

The System.Windows.Forms.TextRenderer class was developed to provide the same level of support for complex scripts in Windows Forms controls that we expect from the Windows operating system. This TextRenderer is based on the GDI text-rendering API, which uses the Windows Unicode Script Processor (Uniscribe). [from MSDN]

有关更多详细信息,请参阅这篇来自 MSDN 杂志的关于文本呈现的好文章:

Text Rendering: Build World-Ready Apps Using Complex Scripts In Windows Forms Controls

关于c# - PrintPreviewDialog中MeasureString测得的字符串高度与实际打印时不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/301066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com