3

モニターの縦横比を幅と高さの 2 桁で取得したい。たとえば、4 と 3、5 と 4、16 と 9 です。

そのタスクのためのコードを書きました。多分それはそれを行うためのより簡単な方法ですか?たとえば、いくつかのライブラリ関数 =\

/// <summary>
/// Aspect ratio.
/// </summary>
public struct AspectRatio
{
    int _height;
    /// <summary>
    /// Height.
    /// </summary>
    public int Height
    {
        get
        {
            return _height;
        }
    }

    int _width;
    /// <summary>
    /// Width.
    /// </summary>
    public int Width
    {
        get
        {
            return _width;
        }
    }

    /// <summary>
    /// Ctor.
    /// </summary>
    /// <param name="height">Height of aspect ratio.</param>
    /// <param name="width">Width of aspect ratio.</param>
    public AspectRatio(int height, int width)
    {
        _height = height;
        _width = width;
    }
}



public sealed class Aux
{
    /// <summary>
    /// Get aspect ratio.
    /// </summary>
    /// <returns>Aspect ratio.</returns>
    public static AspectRatio GetAspectRatio()
    {
        int deskHeight = Screen.PrimaryScreen.Bounds.Height;
        int deskWidth = Screen.PrimaryScreen.Bounds.Width;

        int gcd = GCD(deskWidth, deskHeight);

        return new AspectRatio(deskHeight / gcd, deskWidth / gcd);
    }

    /// <summary>
    /// Greatest Common Denominator (GCD). Euclidean algorithm. 
    /// </summary>
    /// <param name="a">Width.</param>
    /// <param name="b">Height.</param>
    /// <returns>GCD.</returns>
    static int GCD(int a, int b)
    {
        return b == 0 ? a : GCD(b, a % b);
    }

}

4

2 に答える 2

3
  1. クラスを使用Screenして高さ/幅を取得します。
  2. 割ってGCDを得る
  3. 比率を計算します。

次のコードを参照してください。

private void button1_Click(object sender, EventArgs e)
{
    int nGCD = GetGreatestCommonDivisor(Screen.PrimaryScreen.Bounds.Height, Screen.PrimaryScreen.Bounds.Width);
    string str = string.Format("{0}:{1}", Screen.PrimaryScreen.Bounds.Height / nGCD, Screen.PrimaryScreen.Bounds.Width / nGCD);
    MessageBox.Show(str);
}

static int GetGreatestCommonDivisor(int a, int b)
{
    return b == 0 ? a : GetGreatestCommonDivisor(b, a % b);
}
于 2010-04-02T05:47:57.530 に答える
0

それを行うためのライブラリ関数はないと思いますが、そのコードは良さそうです。Javascript で同じことを行うこの関連記事の回答と非常によく似ています: Javascript Aspect Ratio

于 2010-04-02T05:35:41.137 に答える