PrintPreviewDialog中MeasureString测量的字符串高度与实际打印时不同
我正在尝试测量某些文本的高度以用于表格打印目的。
这是代码。 就我而言,它在预览和实际页面上打印不同的数字。 我现在无法尝试除 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);
}
I'm trying to measure height of some text for table printing purpose.
Here's the code. In my case it prints different numbers in preview and on actual page.
I can't try on any printers other than Microsoft Office Document Image Writer right now, but I'm pretty sure it isn't a printer issue.
Perhaps somebody have found a workaround for this problem?
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜问题是 System.Drawing.Graphics 基于 GDI+,而实际打印是基于 GDI 的。
您可以替换对 MeasureString 的调用以使用基于 GDI 的方法:
有关更多详细信息,请参阅 MSDN 杂志上的这篇好文章关于文本渲染:
I guess that the problem is that System.Drawing.Graphics is based on GDI+ where as the actual printing is based on GDI.
You could replace the call to MeasureString to use a GDI based method:
For further details see this good article from MSDN magazine on text rendering:
在 PrintPage 事件中尝试使用 Graphics.Pixel 以外的 PageUnit。 英寸、毫米或点(以及其他)应该在预览或打印中给出相同的结果。 像素 我希望不会,因为预览屏幕和打印机具有不同的像素分辨率。
Try a PageUnit other than Graphics.Pixel in your PrintPage event. Inch, Millimeter or Point (among others) should give you the same result in Preview or printed out. Pixel I would expect not to, since the preview screen and the printer have different pixel resolutions.
我想我可以解决你的问题...我知道如何解决它,更不太明白它的原因。
问题在于字符之间的间距。 例如,如果您在打印时使用单词“VA”(或者当您使用“Graphics.FromImage”时),则字母“A”将在字母“V”之后开始...在“PrintPreview”中,字母“A”从字母“V”内部开始...如果将“PrintPreview”中的各个字母相加,您将获得相同的打印结果!
I think I could solve your problem ... I know how to fix it, more did not quite understand the cause of it.
The problem is the spacing between characters. If you use, for example, the word "VA" on her printing (or when you 'Graphics.FromImage') the letter 'A' starts right after the letter 'V' ... In 'PrintPreview' the letter 'A 'begins just inside the letter' V '... If you add up the individual letters in' PrintPreview ', you will reach the same result printing!
我已经尝试了所有可用的 PageUnit,但没有什么区别。 预览高度与打印高度之比约为1.029336,并且是恒定的。
附言。 实际上我已经找到了解决方法。 我使用 MeasureString 来计算行数,然后将其乘以从 Font 类派生的字符高度,以计算文本块的高度。 经过一些调整后效果很好。
I've tried all PageUnits available but it made no difference. Preview height to print height ratio is about 1.029336 and it is constant.
PS. Actually I've found workaround. I use MeasureString to count number of lines and then multiply it by character height, derived from Font class, to count height of the text chunk. It works nicely with some tweaking.