2011-12-28 42 views
1

我正在爲我的照片作品組建一個相冊。但我在以下問題上自問:縮略圖和相冊

是否應該將縮略圖版本保存在服務器上的文件系統上,還是應該在請求縮略圖時動態生成縮略圖?

我猜存儲縮略圖的專業人員在服務器上的處理較少,因爲它只生成一次文件(上傳文件時)。但缺點是,如果我決定有一天縮略圖大小不對,我有一個問題。此外,我現在存儲2個文件(拇指加原件)。

那麼,縮略圖是否存在最佳尺寸?我想答案是 - 你想存儲多少縮略圖。我的問題是,我的thumnails正在調整到最高150或最高150寬。但是,這些文件的大小仍然在4萬左右。

我使用這個:

public void ResizeImage(string originalFile, string newFile, int newWidth, int maxHeight, bool onlyResizeIfWider) 
    { 
     System.Drawing.Image fullsizeImage = System.Drawing.Image.FromFile(MapPath(GlobalVariables.UploadPath + "/" + originalFile)); 

     // Prevent using images internal thumbnail 
     fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); 
     fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); 

     if (onlyResizeIfWider) 
     { 
      if (fullsizeImage.Width <= newWidth) 
      { 
       newWidth = fullsizeImage.Width; 
      } 
     } 

     int newHeight = fullsizeImage.Height * newWidth/fullsizeImage.Width; 
     if (newHeight > maxHeight) 
     { 
      // Resize with height instead 
      newWidth = fullsizeImage.Width * maxHeight/fullsizeImage.Height; 
      newHeight = maxHeight; 
     } 

     System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero); 

     // Clear handle to original file so that we can overwrite it if necessary 
     fullsizeImage.Dispose(); 
     // Save resized picture 
     newImage.Save(MapPath(GlobalVariables.UploadPath + "/" + newFile)); 
    } 

有沒有減少文件大小的方式,如150高/ 150寬是非常小的。我想上升到250左右,但如果我顯示12個拇指......需要一段時間才能加載。

回答

2

是絕對必須保存爲縮略圖文件而不是一直處理。

關於圖像,嘗試使它絕對適合8×8或16×16的數組塊,因爲這是jpeg用來分割和壓縮它的大小。例如不使它150×150因爲八分之一百五十零= 18.75,用152x152因爲8分之152= 19個

然後降低文件的大小的屬性和更改質量是

g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 

,這裏是一個例子的How to: Use Interpolation Mode to Control Image Quality During Scaling

+0

感謝您的信息塊。很有幫助。有了質量,我使用了System.Drawing.Image - 而這個對象類型沒有提到的屬性。我應該使用位圖來代替,然後一直保存爲Jpeg,一旦使用了上述的屬性? – Craig 2011-12-28 06:08:35

+0

@cdotlister是使用更好的更詳細的縮略圖程序,你可以控制更多的想法。 – Aristos 2011-12-28 06:25:41

+0

謝謝 - 我不確定如何在我的位圖上使用這些屬性... BitmapObject.InterpolationMode不起作用。 – Craig 2011-12-28 06:40:11