C# 获取监视器的纵横比
我想得到显示器的纵横比是两位数:宽度和高度。例如4和3,5和4,16和9 我为那个任务写了一些代码。也许这是更简单的方法?例如,一些库函数=\C# 获取监视器的纵横比,c#,winforms,aspect-ratio,C#,Winforms,Aspect Ratio,我想得到显示器的纵横比是两位数:宽度和高度。例如4和3,5和4,16和9 我为那个任务写了一些代码。也许这是更简单的方法?例如,一些库函数=\ /// <summary> /// Aspect ratio. /// </summary> public struct AspectRatio { int _height; /// <summary> /// Height. /// </summary> publi
/// <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);
}
//
///纵横比。
///
公共结构方面
{
内部高度;
///
///高度。
///
公共内部高度
{
得到
{
返回高度;
}
}
内部宽度;
///
///宽度。
///
公共整数宽度
{
得到
{
返回宽度;
}
}
///
///克托尔。
///
///高宽比的高度。
///宽高比的宽度。
公共方面(内部高度、内部宽度)
{
_高度=高度;
_宽度=宽度;
}
}
公共密封等级Aux
{
///
///获取纵横比。
///
///纵横比。
公共静态AspectRatio GetAspectRatio()
{
int deskHeight=Screen.PrimaryScreen.Bounds.Height;
int deskWidth=Screen.PrimaryScreen.Bounds.Width;
int gcd=gcd(桌面宽度、桌面高度);
返回新AspectRatio(桌面高度/gcd、桌面宽度/gcd);
}
///
///最大公分母(GCD)。欧几里德算法。
///
///宽度。
///高度。
///GCD。
静态整数GCD(整数a、整数b)
{
返回b==0?a:GCD(b,a%b);
}
}我认为没有库函数可以实现这一点,但代码看起来不错。与此相关帖子中关于使用Javascript执行相同操作的回答非常相似:
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);
}
-1:我不同意
的想法,这是不可能的。正如我所想,没有库函数。无论如何,谢谢你的回答;)我的屏幕分辨率是1813x1024,不知道为什么返回“1024:1813”?我的屏幕分辨率是1366*768,但返回384:683!我能做什么