如何从图像尺寸计算纵横比

时间:2013-06-17 17:32:56

标签: c# javascript image aspect-ratio

我正在使用Jcrop裁剪图像,所以我想计算图像高度和宽度的比例,但问题是没有最大高度和宽度的限制。

当用户上传图片然后我想获得高度,宽度比等所以裁剪它应该是关于纵横比裁剪

  

宽度= 835,高度= 625纵横比为167:125

我从以下链接计算了这个比率 Aspect ratio calculator

我不想重新考虑新的高度,宽度。我只想计算比例167:125

我该怎么做?

1 个答案:

答案 0 :(得分:5)

我认为你正在寻找HCF(最高公因数)但比例(宽度:835,身高:625)将是167:125。 以下是可以计算2个数字之间的HCF的函数。

 private int FindHCF(int m, int n)
 {
     int temp, remainder;
     if (m < n)
     {
         temp = m;
         m = n;
         n = temp;
     }
     while (true)
     {
         remainder = m % n;
         if (remainder == 0)
             return n;
         else
             m = n;
         n = remainder;
     }
 }

所以这是代码的其余部分

int hcf = FindHcf(835, 625);
int factorW = 835 / hcf;
int factorH = 625 / hcf;