25

データベースが実際に表示サイズを twip で保存する移行プロジェクトに取り組んでいます。twips を使用して WPF または Winforms コントロールにサイズを割り当てることができないため、実行時に使用できる変換メソッドが .NET にあるかどうか疑問に思っていました。

4

4 に答える 4

30

移行ツールには何かがあることがわかりましたが、実行時には何の役にも立たないでしょう。これが私がしたことです(拡張メソッドのハードコードされた値がポイント/インチの値に変更された場合、ポイントコンバーターとしても機能します):

1 ツイップ = 1/1440 インチ。.NETGraphicsオブジェクトには と メソッドがDpiXあり、DpiYこれを使用して 1 インチが何ピクセルかを判断できます。これらの測定値を使用して、次の拡張メソッドを作成しました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ではフォームであるため、のスーパークラスCreateGraphicsから継承されます)。FormControl

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

グラフィックス オブジェクトを取得する方法の一覧については、Graphicsクラスのドキュメントの「備考」セクションを参照してください。より完全なドキュメントは、チュートリアルHow to: Create Graphics Objects にあります。

最も簡単な方法の簡単な要約:

  • Control.CreateGraphics
  • Paint イベントのプロパティにPaintEventArgsは がGraphicsありGraphicsます。
  • 画像を渡すと、その画像に描画できるオブジェクトGraphics.FromImageが返されます。Graphics(注: 実際の画像に twip を使用することはほとんどありません)
于 2010-10-28T15:28:10.607 に答える
6

移行プロジェクトでは、組み込みの変換サポート関数を使用できます

microsoft.visualbasic.compatibility.vb6.twipsperpixelx
microsoft.visualbasic.compatibility.vb6.twipsperpixely
于 2012-07-15T04:35:08.547 に答える
4

Graphics参考までに、オブジェクトを要求する代わりに Win32 API を使用する別の C# バージョン:

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: Twips をピクセルに変換する方法

于 2010-12-14T03:58:12.913 に答える