如何在 .NET 中将缇转换为像素? [英] How do I convert Twips to Pixels in .NET?

查看:31
本文介绍了如何在 .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?

推荐答案

事实证明迁移工具有一些东西,但它在运行时没有任何好处.这是我所做的(如果将扩展方法中的硬编码值更改为每英寸点数的值,它也可以用作点转换器):

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 Graphics 对象有一个方法 DpiXDpiY 可用于确定一英寸中有多少像素.使用这些测量,我为 Graphics 创建了以下扩展方法:

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 对象(此处为 this 是一个 Form,所以 CreateGraphics 继承自 Form 的超类 Control):

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);
}

请参阅 Graphics 中的备注"部分 类文档 获取图形对象的方法列表.教程如何:创建图形对象中提供了更完整的文档.

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 在其 Graphics 属性中有一个 Graphics 可用.
  • Hand Graphics.FromImage 一个图像,它将返回一个可以在该图像上绘制的 Graphics 对象.(注意:您不太可能希望对实际图像使用缇)
  • Control.CreateGraphics
  • a Paint event's PaintEventArgs has a Graphics available in its Graphics property.
  • Hand Graphics.FromImage an image and it will return a Graphics object that can draw on that image. (NOTE: It is unlikely that you'll want to use twips for an actual image)

这篇关于如何在 .NET 中将缇转换为像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆