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

发布于 2024-09-29 18:40:58 字数 98 浏览 6 评论 0原文

我正在开发一个迁移项目,其中数据库实际上以缇为单位存储显示尺寸。 由于我无法使用缇为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故人如初 2024-10-06 18:40:58

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

1 Twip = 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 返回 CreateGraphics 的上下文中) code>Drawing.Graphics 对象(这里 this 是一个 Form,因此 CreateGraphics 继承自 Form 的超类< code>Control):

using( Graphics g = CreateGraphics() )
{
    Width = g.ConvertTwipsToXPixels(sizeInTwips);
    Height = g.ConvertTwipsToYPixels(sizeInTwips);
}

请参阅 Graphics 类文档,获取获取图形对象的方法列表。教程 如何:创建图形对象

最简单方法的简要总结:

  • Control.CreateGraphics
  • Paint 事件的 PaintEventArgsGraphics 属性中有一个 Graphics 可用,
  • Hand 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 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);
    }
}

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

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
  • 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)
北方的韩爷 2024-10-06 18:40:58

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

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

For migration projects we can use built in converstion support functions

microsoft.visualbasic.compatibility.vb6.twipsperpixelx
microsoft.visualbasic.compatibility.vb6.twipsperpixely
浪菊怪哟 2024-10-06 18:40:58

作为参考,另一个 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,因为不能保证像素始终是正方形(根据知识库)。

参考:kb210590:ACC2000:如何将缇转换为像素

For reference, another C# version, using the Win32 API instead of requiring a Graphics objects:

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
}

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

郁金香雨 2024-10-06 18:40:58

我知道旧帖子,但这里仅供参考,还有上面 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/1440th of an inch. A twip is 1/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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文