Получить соотношение сторон монитора

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

}


person Alexander Stalt    schedule 02.04.2010    source источник


Ответы (2)


  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);
}
person KMån    schedule 02.04.2010
comment
Как я и думал, библиотечной функции нет. Все равно спасибо за ответ ;) - person Alexander Stalt; 05.04.2010
comment
Мое разрешение экрана 1813x1024, и я не уверен, почему оно вернуло 1024:1813? - person Jayson Ragasa; 19.10.2012
comment
Разрешение моего экрана 1366 * 768, но оно возвращает 384: 683! что я могу сделать - person user3290286; 23.02.2014

Я не думаю, что для этого есть библиотечная функция, но этот код выглядит хорошо. Очень похоже на ответ в этом связанном посте о том, как делать то же самое в Javascript: need-an-output-like-43-169">Соотношение сторон JavaScript

person cpalmer    schedule 02.04.2010
comment
-1: Я бы не согласился с thought, что это невозможно. - person KMån; 02.04.2010