【发布时间】:2018-10-27 16:26:04
【问题描述】:
我想确定一个源分辨率是否属于指定的纵横比,在 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 的纵横比。我没有充分了解如何计算这个的基础知识。
【问题讨论】:
标签: c# .net vb.net aspect-ratio imaging