如何转换为缇像素.NET? [英] How do I convert Twips to Pixels in .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?
推荐答案
原来,迁移工具拥有的东西,但它不会做任何好处在运行时。这是我做了(如果在扩展方法的硬codeD值改为每英寸点数,将工作作为一个点转换器值太):
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缇= 1 /英寸的1440。
在.NET 图形
对象有一个方法 DpiX
和 DpiY
它可以用来确定有多少像素处于一英寸。
使用这些测量我创建了图形
下面的扩展方法:
1 Twip = 1/1440th of an inch.
The .NET Graphics
object has a method DpiX
and DpiY
which can be used to determine how many pixels are in an inch.
Using those measurements I created the following extension methods for Graphics
:
using System.Drawing;
static class Extensions
{
/// <summary>
/// Converts an integer value in twips to the corresponding integer value
/// in pixels on the x-axis.
/// </summary>
/// <param name="source">The Graphics context to use</param>
/// <param name="inTwips">The number of twips to be converted</param>
/// <returns>The number of pixels in that many twips</returns>
public static int ConvertTwipsToXPixels(this Graphics source, int twips)
{
return (int)(((double)twips) * (1.0 / 1440.0) * source.DpiX);
}
/// <summary>
/// Converts an integer value in twips to the corresponding integer value
/// in pixels on the y-axis.
/// </summary>
/// <param name="source">The Graphics context to use</param>
/// <param name="inTwips">The number of twips to be converted</param>
/// <returns>The number of pixels in that many twips</returns>
public static int ConvertTwipsToYPixels(this Graphics source, int twips)
{
return (int)(((double)twips) * (1.0 / 1440.0) * source.DpiY);
}
}
要使用这些方法之一,只是需要做以下(假设你在的createGraphics
返回 Drawing.Graphics 上下文是code>对象(这里
这个
是一个形式,让的createGraphics
是表格
的超一流控制
)
To use these methods one simply has to do the following (assuming you're in a context where CreateGraphics
returns a Drawing.Graphics
object (here this
is a Form, so CreateGraphics
is inherited from Form
's super-class Control
):
using( Graphics g = CreateGraphics() )
{
Width = g.ConvertTwipsToXPixels(sizeInTwips);
Height = g.ConvertTwipsToYPixels(sizeInTwips);
}
请参见 图形<备注部分/ code>的方式来获得图形对象的列表类文档
。更完整的文档可在本教程的如何:创建图形对象
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.
的最简单的方法小结:
-
Control.CreateGraphics
- Paint事件的
PaintEventArgs的
有一个图形
在图形$ C可$ C>属性。
- 手动
Graphics.FromImage
图像,它会返回一个图形
对象,可以是图片上画画。 (注:这是不可能的,你需要使用缇为实际图像)
Control.CreateGraphics
- a Paint event's
PaintEventArgs
has aGraphics
available in itsGraphics
property. - Hand
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)
这篇关于如何转换为缇像素.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!