[ACCEPTED]-How do I convert Twips to Pixels in .NET?-screen-resolution

Accepted answer
Score: 30

It turns out that the migration tool has 18 something, but it wouldn't do any good at 17 runtime. Here's what I did (if the hard-coded 16 value in the extension method were changed 15 to the value for points per inch it would 14 work as a point converter too):

1 Twip = 1/1440th 13 of an inch. The .NET Graphics object has a method 12 DpiX and DpiY which can be used to determine how 11 many pixels are in an inch. Using those 10 measurements I created the following extension 9 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 8 has to do the following (assuming you're 7 in a context where CreateGraphics returns a Drawing.Graphics object (here 6 this is a Form, so CreateGraphics is inherited from Form's super-class 5 Control):

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

See the "Remarks" section in 4 the Graphics Class documentation for a list of ways to obtain a graphics 3 object. More complete documentation is 2 available in the tutorial How to: Create Graphics Objects.

Brief summary 1 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)
Score: 6

For migration projects we can use built 1 in converstion support functions

microsoft.visualbasic.compatibility.vb6.twipsperpixelx
microsoft.visualbasic.compatibility.vb6.twipsperpixely
Score: 4

For reference, another C# version, using 4 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 3 MeasureDirection is supposed to be specified as there is 2 no guarantee that pixels are always square 1 (according to the kb).

Reference: kb210590: ACC2000: How to Convert Twips to Pixels

Score: 2

Old post I know but here is an FYI and some 9 math for the 1440 answers above...

A twip 8 is not simply 1/1440th of an inch. A twip is 1/20 of 7 a point.

The point you are working with in a 6 printable document, typically, is a postscript 5 72dpi:

72dpi * 20Twips/Point = 1440twips.

So in dealing with say a Crystal 4 report, with a twips width of 15840 (and margins 3 of 0 twips), the width would be 11 inches (15840 / (72 * 20)).

Based 2 on a screen density of 96 dpi, the report 1 will post to the screen at:

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

More Related questions