如何在 .NET 中将缇转换为像素?
我正在开发一个迁移项目,其中数据库实际上以缇为单位存储显示尺寸。 由于我无法使用缇为 WPF 或 Winforms 控件分配大小,我想知道 .NET 是否有可在运行时使用的转换方法?
I'm working on a migration project in which a database actually stores display sizes in twips.
Since I can't use twips to assign sizes to WPF or Winforms controls, I was wondering if .NET has a conversion method usable at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实证明,迁移工具有一些东西,但它在运行时没有任何用处。这就是我所做的(如果扩展方法中的硬编码值更改为每英寸点数的值,它也可以用作点转换器):
1 Twip = 1/1440 英寸。
.NET
Graphics
对象具有方法DpiX
和DpiY
,可用于确定一英寸中有多少个像素。使用这些测量值,我为
Graphics
创建了以下扩展方法:要使用这些方法,只需执行以下操作(假设您处于
CreateGraphics
返回CreateGraphics
的上下文中) code>Drawing.Graphics 对象(这里this
是一个 Form,因此CreateGraphics
继承自Form
的超类< code>Control):请参阅
Graphics
类文档,获取获取图形对象的方法列表。教程 如何:创建图形对象。最简单方法的简要总结:
Control.CreateGraphics
PaintEventArgs
的Graphics
属性中有一个Graphics
可用,Graphics.FromImage
图像,它将返回一个Graphics<。 /code> 可以在该图像上绘制的对象。 (注意:您不太可能想要对实际图像使用缇)
It turns out that the migration tool has something, but it wouldn't do any good at runtime. Here's what I did (if the hard-coded value in the extension method were changed to the value for points per inch it would work as a point converter too):
1 Twip = 1/1440th of an inch.
The .NET
Graphics
object has a methodDpiX
andDpiY
which can be used to determine how many pixels are in an inch.Using those measurements I created the following extension methods for
Graphics
:To use these methods one simply has to do the following (assuming you're in a context where
CreateGraphics
returns aDrawing.Graphics
object (herethis
is a Form, soCreateGraphics
is inherited fromForm
's super-classControl
):See the "Remarks" section in the
Graphics
Class documentation for a list of ways to obtain a graphics object. More complete documentation is available in the tutorial How to: Create Graphics Objects.Brief summary of easiest ways:
Control.CreateGraphics
PaintEventArgs
has aGraphics
available in itsGraphics
property.Graphics.FromImage
an image and it will return aGraphics
object that can draw on that image. (NOTE: It is unlikely that you'll want to use twips for an actual image)对于迁移项目,我们可以使用内置的转换支持功能
For migration projects we can use built in converstion support functions
作为参考,另一个 C# 版本使用 Win32 API,而不需要
Graphics
对象:应该指定
MeasureDirection
,因为不能保证像素始终是正方形(根据知识库)。参考:kb210590:ACC2000:如何将缇转换为像素
For reference, another C# version, using the Win32 API instead of requiring a
Graphics
objects:The
MeasureDirection
is supposed to be specified as there is no guarantee that pixels are always square (according to the kb).Reference: kb210590: ACC2000: How to Convert Twips to Pixels
我知道旧帖子,但这里仅供参考,还有上面 1440 个答案的一些数学计算……
缇并不是简单的 1/1440 英寸。缇是点的
1/20
。您在可打印文档中使用的点通常是 postscript 72dpi:
72dpi * 20Twips/Point = 1440twips
。因此,在处理一个 Crystal 报表时,其宽度为 15840 缇(边距为 0 缇),
宽度为
11 英寸 (15840 / (72 * 20))
。根据 96 dpi 的屏幕密度,报告将发布到屏幕上:
1056 像素宽 (96dpi * 11 英寸 = 1056 像素)
。Old post I know but here is an FYI and some math for the 1440 answers above...
A twip is not simply
1/1440
th of an inch. A twip is1/20
of a point.The point you are working with in a printable document, typically, is a postscript 72dpi:
72dpi * 20Twips/Point = 1440twips
.So in dealing with say a Crystal report, with a twips width of
15840
(and margins of 0 twips),the width would be
11 inches (15840 / (72 * 20))
.Based on a screen density of 96 dpi, the report will post to the screen at:
1056px wide (96dpi * 11in = 1056px)
.