Получить соотношение сторон монитора - PullRequest
3 голосов
/ 02 апреля 2010

Я хочу получить соотношение сторон монитора в виде двух цифр: ширины и высоты. Например 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);
    }

}

Ответы [ 2 ]

2 голосов
/ 02 апреля 2010
  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);
}
0 голосов
/ 02 апреля 2010

Я не думаю, что есть библиотечная функция для этого, но этот код выглядит хорошо.Очень похоже на ответ в этом связанном посте о том же: Javascript: Javascript Aspect Ratio

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...