【问题标题】:Determine whether a source resolution belongs to the specified aspect ratio判断一个源分辨率是否属于指定的纵横比
【发布时间】: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


    【解决方案1】:

    您的计算有误。单独修改每个值不会验证纵横比。

    例如

    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
    

    【讨论】:

    • 我知道在 cmets 框中感谢某人是不受欢迎的,但我还是谢谢你。
    • 我刚刚在这里阅读了一篇文章,可能会发生变化。我们可能被允许成为人类(简短地)说“请”和“谢谢”。
    猜你喜欢
    • 2021-11-24
    • 2013-01-19
    • 1970-01-01
    • 2012-10-06
    • 2011-10-27
    • 2011-12-29
    • 2014-07-28
    • 1970-01-01
    • 2021-04-23
    相关资源
    最近更新 更多