2010-04-02 2 views
3

모니터의 종횡비를 너비와 높이의 두 자릿수로 가져 오려고합니다. 예를 들어 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); 
    } 

}

답변

1
  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

제가 생각하기에 라이브러리 기능은 없습니다. 어쨌든 답변 주셔서 감사합니다;) –

+0

내 화면 해상도는 1813x1024이며 왜 "1024 : 1813"을 반환했는지 확신 할 수 없습니까? –

+0

내 화면 해상도는 1366 * 768이지만 384 : 683을 반환합니다! 내가 무엇을 할 수 있을지 – user3290286

0

내가 할 수있는 라이브러리 함수가 있다고 생각하지 않지만, 그 코드가 좋아 보인다. 자바 스크립트에서 같은 일을하고이 관련 게시물에 대답 매우 유사 : Javascript Aspect Ratio

+0

-1 : 나는'이 불가능하다고 thought'에 동의하지 않을 것입니다. –

관련 문제