2011-04-19 140 views
0

我需要一种方法来计算图像的宽度和高度值调整到1024px时如何在调整大小时计算图像的宽度和高度值?

图像的最大值,高度或宽度将被调整为1024px,我需要找出剩余的宽度或高度值。

调整大小时,图像(3200 x 2400px)转换为(1024 x 768px)。

这需要是动态的,因为一些图像将是人像和一些风景。

任何人都可以建议如何我工作的解决方案为以下:

<msxsl:script language="C#" implements-prefix="emint"> 
    <![CDATA[public string GetExtension(string fileName) 
     { 
     string[] terms = fileName.Split('.'); 
     if (terms.Length <= 0) 
     { 
     return string.Empty; 
     } 
     return terms[terms.Length -1]; 
     } 

     public string GetFileName(string fileName) 
     { 
     string[] terms = fileName.Split('/'); 
     if (terms.Length <= 0) 
     { 
     return string.Empty; 
     } 
     return terms[terms.Length -1]; 
     } 

     public string GetFileSize(Decimal mbs) 
     { 
     Decimal result = Decimal.Round(mbs, 2); 
     if (result == 0) 
     { 
     result = mbs * 1024; 
     return Decimal.Round(result, 2).ToString() + " KB"; 
     } 
     return result.ToString() + " MB"; 
     } 

     public string GetCentimeters(Decimal pix) 
     { 
     Decimal formula = (decimal)0.026458333; 
     Decimal result = pix * formula; 
     return Decimal.Round(result,0).ToString(); 
     }]]> 
    </msxsl:script> 
+0

可能重复的[如何“智能调整大小”所显示的图像以原始长宽比](http://stackoverflow.com/questions/3008772/how-to-smart-resize-a-displayed-image -to-原始长宽比) – PleaseStand 2011-04-19 02:32:28

回答

2
  width = 1024; 
      height = 768; 

      ratio_orig = width_orig/height_orig; 

      if (width/height > ratio_orig) { 
      width = height*ratio_orig; 
      } else { 
      height = width/ratio_orig; 
      } 

在年底的widthheight值对应于图像的宽度和高度。这保持了宽高比。

0

这里是一个伪代码算法。它将选择与原始图像具有相同宽度/高度比例的最大可能尺寸(小于1024x1024图像)。

target_width = 1024 
target_height = 1024 

target_ratio = target_width/target_height 
orig_ratio = orig_width/orig_height 

if orig_ratio < target_ratio 
    # Limited by height 
    target_width = round(target_height * orig_ratio) 
else 
    # Limited by width 
    target_height = round(target_width/orig_ratio) 
相关问题