vb.net 将印刷单位转换为 MM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22453628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:07:21  来源:igfitidea点击:

Convert Printing Units to MM

.netvb.netvb.net-2010units-of-measurementsystem.printing

提问by Graham Jones

I have my printer set to MM. I am trying to work out how to centre text when printing using the following code:

我的打印机设置为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)

Now I know it's a matter of units and I think one is in MM but I am not sure what the other unit is, so I can not work out a conversion factor for it.

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

Any Ideas?

有任何想法吗?

回答by Hans Passant

The default Graphics.PageUnit you get in your PrintPage event handler is GraphicsUnit.Display. Which maps 100 pixels to an inch. It is a convenience setting, it makes objects you draw on paper about the same size as you when you draw them to the screen. Video adapters commonly run in 96 dots-per-inch resolution. Helps you to write graphics code that works both for the printer and the screen.

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

You can simply reassign it. You of course are looking for GraphicsUnit.Millimeter.

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

Do keep in mind that this has no effect whatsoever on centering text. Which of course works just as well when you measure in millimeters as in 0.01 inches.

请记住,这对居中文本没有任何影响。当然,当您以毫米为单位和 0.01 英寸进行测量时,这当然同样有效。

回答by Graham Jones

After a lot of fiddling I've done it:

经过大量的摆弄,我做到了:

Basically I set the Graphics Unit to Millimetres. Even though this has been set the page bounds still return in pixels, BUT the Measure string returns Millimetres.

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

After a lot of trial and error I found out that to convert the Page Bounds to millimetres you have to times it by 0.264583333, this is the size of a pixel in MM.

经过大量试验和错误,我发现要将页面边界转换为毫米,您必须将其乘以 0.264583333,这是以 MM 为单位的像素大小。

Here is the code:

这是代码:

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)

I hope this helps others.

我希望这对其他人有帮助。