如何在.NET中将Twips转换为像素?
我正在进行一个迁移项目,其中数据库实际上以缇为单位存储显示大小。 由于我不能使用twips为WPF或Winforms控件分配大小,我想知道.NET是否有在运行时可用的转换方法?
事实certificate,迁移工具有一些东西,但它在运行时不会有任何好处。 这就是我所做的(如果扩展方法中的硬编码值更改为每英寸点的值,它也将用作点转换器):
1缇= 1/1440英寸。 .NET Graphics
对象有一个方法DpiX
和DpiY
,可用于确定一英寸的像素数。 使用这些测量我为Graphics
创建了以下扩展方法:
using System.Drawing; static class Extensions { /// /// Converts an integer value in twips to the corresponding integer value /// in pixels on the x-axis. /// /// The Graphics context to use /// The number of twips to be converted /// The number of pixels in that many twips public static int ConvertTwipsToXPixels(this Graphics source, int twips) { return (int)(((double)twips) * (1.0 / 1440.0) * source.DpiX); } /// /// Converts an integer value in twips to the corresponding integer value /// in pixels on the y-axis. /// /// The Graphics context to use /// The number of twips to be converted /// The number of pixels in that many twips public static int ConvertTwipsToYPixels(this Graphics source, int twips) { return (int)(((double)twips) * (1.0 / 1440.0) * source.DpiY); } }
要使用这些方法,只需执行以下操作(假设您处于CreateGraphics
返回Drawing.Graphics
对象的上下文中(此处为Form,因此CreateGraphics
inheritance自Form
的超类Control
):
using( Graphics g = CreateGraphics() ) { Width = g.ConvertTwipsToXPixels(sizeInTwips); Height = g.ConvertTwipsToYPixels(sizeInTwips); }
有关获取图形对象的方法列表,请参阅Graphics
Class文档中的“备注”部分。 教程如何:创建图形对象中提供了更完整的文档。
最简单方法的简要总结:
-
Control.CreateGraphics
- Paint事件的
PaintEventArgs
在其Graphics
属性中有一个Graphics
。 - Hand
Graphics.FromImage
一个图像,它将返回一个可以在该图像上绘制的Graphics
对象。 (注意:您不太可能想要使用缇实际图像)
作为参考,另一个C#版本,使用Win32 API而不是需要Graphics
对象:
using System.Runtime.InteropServices; static class SomeUtils { public static int ConvertTwipsToPixels(int twips, MeasureDirection direction) { return (int)(((double)twips)*((double)GetPixperinch(direction))/1440.0); } public static int ConvertPixelsToTwips(int pixels, MeasureDirection direction) { return (int)((((double)pixels)*1440.0)/((double)GetPixperinch(direction))); } public static int GetPPI(MeasureDirection direction) { int ppi; IntPtr dc = GetDC(IntPtr.Zero); if (direction == MeasureDirection.Horizontal) ppi = GetDeviceCaps(dc, 88); //DEVICECAP LOGPIXELSX else ppi = GetDeviceCaps(dc, 90); //DEVICECAP LOGPIXELSY ReleaseDC(IntPtr.Zero, dc); return ppi; } [DllImport("user32.dll")] static extern IntPtr GetDC(IntPtr hWnd); [DllImport("user32.dll")] static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("gdi32.dll")] static extern int GetDeviceCaps(IntPtr hdc, int devCap); } public enum MeasureDirection { Horizontal, Vertical }
应该指定MeasureDirection
,因为不能保证像素总是正方形(根据kb)。
参考: kb210590:ACC2000:如何将缇转换为像素
对于迁移项目,我们可以使用内置的转换支持function
microsoft.visualbasic.compatibility.vb6.twipsperpixelx microsoft.visualbasic.compatibility.vb6.twipsperpixely
老post我知道,但这里是一个FYI和一些数学上面的1440答案……
1/1440
不仅仅是1/1440
英寸。 缇是1/20
的一点 。
您在可打印文档中使用的要点通常是后记72dpi:
72dpi * 20Twips/Point = 1440twips
。
因此,在处理Crystal报告时,缇宽度为15840
(边距为0缇),宽度为11 inches (15840 / (72 * 20))
。
根据96 dpi的屏幕密度,报告将发布到屏幕:
1056px wide (96dpi * 11in = 1056px)
。