判断源分辨率是否属于指定宽高比
Determine whether a source resolution belongs to the specified aspect ratio
我想确定一个源分辨率是否属于指定的宽高比,在C#或VB.NET。
目前我写的是:
/// --------------------------------------------------------------------------
/// <summary>
/// Determine whether the source resolution belongs to the specified aspect ratio.
/// </summary>
/// --------------------------------------------------------------------------
/// <param name="resolution">
/// The source resolution.
/// </param>
///
/// <param name="aspectRatio">
/// The aspect ratio.
/// </param>
/// --------------------------------------------------------------------------
/// <returns>
/// <see langword="true"/> if the source resolution belongs to the specified aspect ratio;
/// otherwise, <see langword="false"/>.
/// </returns>
/// ----------------------------------------------------------------------------------------------------
public static bool ResolutionIsOfAspectRatio(Size resolution, Point aspectRatio) {
return (resolution.Width % aspectRatio.X == 0) &&
(resolution.Height % aspectRatio.Y == 0);
}
VB.NET:
Public Shared Function ResolutionIsOfAspectRatio(resolution As Size,
aspectRatio As Point) As Boolean
Return ((resolution.Width Mod aspectRatio.X) AndAlso
(resolution.Height Mod aspectRatio.Y)) = 0
End Function
用法示例:
Size resolution = new Size(1920, 1080);
Point aspectRatio = new Point(16, 9);
bool result = ResolutionIsOfAspectRatio(resolution, aspectRatio);
Console.WriteLine(result);
我只是想确保我没有遗漏任何纵横比概念,在使用我编写的函数时可能会导致意外结果。
那么,我的问题是:该算法是否适用于所有情况?如果不是,我应该做哪些修改才能正确执行此操作?
编辑:我注意到该算法完全错误,它采用 640x480 作为 16:9 纵横比。我没有充分了解如何计算这个的基础知识。
您的计算有误。仅 mod 每个值单独不验证纵横比。
例如
Size res = new Size(1920, 1080);
Point aspect = new Point(16, 9); //1080%9==0 valid and true
bool result = (res.Width % aspect.X == 0) &&(res.Height % aspect.Y == 0);
是正确的,但
Point aspect = new Point(16, 10); //1080%10==0 invalid and true!
也是如此。
正确的计算方式是
bool result = res.Width / aspect.X == res.Height / aspect.Y; //1920/16 == 1080/9
我想确定一个源分辨率是否属于指定的宽高比,在C#或VB.NET。
目前我写的是:
/// --------------------------------------------------------------------------
/// <summary>
/// Determine whether the source resolution belongs to the specified aspect ratio.
/// </summary>
/// --------------------------------------------------------------------------
/// <param name="resolution">
/// The source resolution.
/// </param>
///
/// <param name="aspectRatio">
/// The aspect ratio.
/// </param>
/// --------------------------------------------------------------------------
/// <returns>
/// <see langword="true"/> if the source resolution belongs to the specified aspect ratio;
/// otherwise, <see langword="false"/>.
/// </returns>
/// ----------------------------------------------------------------------------------------------------
public static bool ResolutionIsOfAspectRatio(Size resolution, Point aspectRatio) {
return (resolution.Width % aspectRatio.X == 0) &&
(resolution.Height % aspectRatio.Y == 0);
}
VB.NET:
Public Shared Function ResolutionIsOfAspectRatio(resolution As Size,
aspectRatio As Point) As Boolean
Return ((resolution.Width Mod aspectRatio.X) AndAlso
(resolution.Height Mod aspectRatio.Y)) = 0
End Function
用法示例:
Size resolution = new Size(1920, 1080);
Point aspectRatio = new Point(16, 9);
bool result = ResolutionIsOfAspectRatio(resolution, aspectRatio);
Console.WriteLine(result);
我只是想确保我没有遗漏任何纵横比概念,在使用我编写的函数时可能会导致意外结果。
那么,我的问题是:该算法是否适用于所有情况?如果不是,我应该做哪些修改才能正确执行此操作?
编辑:我注意到该算法完全错误,它采用 640x480 作为 16:9 纵横比。我没有充分了解如何计算这个的基础知识。
您的计算有误。仅 mod 每个值单独不验证纵横比。
例如
Size res = new Size(1920, 1080);
Point aspect = new Point(16, 9); //1080%9==0 valid and true
bool result = (res.Width % aspect.X == 0) &&(res.Height % aspect.Y == 0);
是正确的,但
Point aspect = new Point(16, 10); //1080%10==0 invalid and true!
也是如此。
正确的计算方式是
bool result = res.Width / aspect.X == res.Height / aspect.Y; //1920/16 == 1080/9