如何在.NET中将Twips转换为像素?

时间:2010-10-28 15:27:07

标签: c# graphics vb6-migration screen-resolution

我正在开展一个迁移项目,其中数据库实际上以缇为单位存储显示大小。 由于我不能使用twips为WPF或Winforms控件分配大小,我想知道.NET是否有在运行时可用的转换方法?

4 个答案:

答案 0 :(得分:28)

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

1缇= 1/1440英寸。 .NET Graphics对象有一个方法DpiXDpiY,可用于确定一英寸的像素数。 使用这些测量,我为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是一个表单,因此{{ 1}}继承自CreateGraphics的超类Form):

Control

有关获取图形对象的方法列表,请参阅Graphics Class documentation中的“备注”部分。教程How to: Create Graphics Objects中提供了更完整的文档。

最简单方法的简要总结:

  • using( Graphics g = CreateGraphics() ) { Width = g.ConvertTwipsToXPixels(sizeInTwips); Height = g.ConvertTwipsToYPixels(sizeInTwips); }
  • 一个Paint事件Control.CreateGraphicsPaintEventArgs属性中有一个Graphics
  • Graphics图像,它将返回可以在该图像上绘制的Graphics.FromImage对象。 (注意:您不太可能想要使用缇实际图像)

答案 1 :(得分:4)

供参考,另一个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: How to Convert Twips to Pixels

答案 2 :(得分:4)

对于迁移项目,我们可以使用内置的转换支持功能

microsoft.visualbasic.compatibility.vb6.twipsperpixelx
microsoft.visualbasic.compatibility.vb6.twipsperpixely

答案 3 :(得分:2)

我知道的旧帖子,但这里是一个FYI和上面1440个答案的数学...

缇不仅仅是1/1440英寸。 twip是{strong>点的1/20

您在可打印文档中使用的要点通常是后记72dpi:

72dpi * 20Twips/Point = 1440twips

所以在处理一个Crystal报告时,缇宽度为15840(边距为0缇), 宽度为11 inches (15840 / (72 * 20))

根据96 dpi的屏幕密度,报告将发布到屏幕:

1056px wide (96dpi * 11in = 1056px)