2011-04-19 122 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) 
相關問題