将打印单位转换为MM

时间:2014-03-17 11:54:55

标签: .net vb.net vb.net-2010 units-of-measurement system.printing

我的打印机设置为MM。 我正在尝试使用以下代码计算如何在打印时居中文本:

Dim CentrePage As Single
CentrePage = Convert.ToString((e.PageBounds.Width / 2) - (e.Graphics.MeasureString("SENpic Report - " & Format(Now, "dddd, dd MMMM, yyyy") & " - Page " & prnPage, f).Width / 2))
e.Graphics.DrawString("SENpic Report - " & Format(Now, "dddd, dd MMMM, yyyy") & " - Page " & prnPage, f, br, CentrePage, 17)

现在我知道这是一个单位的问题,我认为一个是MM,但我不确定其他单位是什么,所以我无法计算出转换因子。

任何想法?

3 个答案:

答案 0 :(得分:3)

您在PrintPage事件处理程序中获得的默认Graphics.PageUnit是GraphicsUnit.Display。将100像素映射到一英寸。它是一种便利设置,它使您在纸上绘制的对象与您将它们绘制到屏幕时的大小相同。视频适配器通常以每英寸96点的分辨率运行。帮助您编写适用于打印机和屏幕的图形代码。

您可以简单地重新分配它。你当然在寻找GraphicsUnit.Millimeter。

请记住,这对于居中文本没有任何影响。当你以毫米为单位测量0.01英寸时,当然也适用。

答案 1 :(得分:1)

经过大量的努力,我完成了它:

基本上我将图形单位设置为毫米。即使已设置此页面边界仍以像素为单位返回,但“测量”字符串将返回毫米。

经过大量的反复试验后,我发现要将Page Bounds转换为毫米,你需要将它的时间乘以0.264583333,这是MM中像素的大小。

以下是代码:

e.Graphics.PageUnit = GraphicsUnit.Millimeter
Dim f As Font = New Font("Vanada", 12)
Dim br As SolidBrush = New SolidBrush(Color.Black)
Dim CentrePage As Single
Dim CentreText As String = "My Report - " & Format(Now, "dddd, dd MMMM, yyyy") & " - Page " & prnPage
CentrePage = Convert.ToString(((e.PageBounds.Width * 0.264583333) / 2) - (e.Graphics.MeasureString(CentreText, f).Width / 2))
e.Graphics.DrawString(CentreText, f, br, Int(CentrePage), 17)

我希望这有助于其他人。

答案 2 :(得分:0)

e.Graphics.PageUnit = GraphicsUnit.Millimeter e.Graphics.DrawString(TextBox1.Text, New Font("ARIAL", 12, FontStyle.Bold or FontStyle.Italic), Brushes.Red, 105, 30, YAZI_ORTA)

相关问题