2012-09-01 91 views
0

我正在嘗試調整通過MVC3應用上傳的圖像大小。下面的代碼可以根據指定的參數根據我的目標尺寸調整圖像的大小,但最終結果是顆粒狀圖像。我在這裏嘗試了很多不同的解決方案,在stackoverflow和msn示例中,但是一切都會返回一個顆粒感的圖像。成像調整大小會導致C#中粒狀輸出#

static public Image ResizeFile(HttpPostedFileBase file, int targeWidth, int targetHeight) 
    { 
     Image originalImage = null; 
     bool IsImage = true; 
     Bitmap bitmap = null; 

     try 
     { 
      originalImage = Image.FromStream(file.InputStream, true, true); 
     } 
     catch 
     { 
      IsImage = false; 
     } 

     if (IsImage) 
     { 
      //accept an image only if it is less than 3mb(max) 
      if (file.ContentLength <= 3145728) 
      { 
       var newImage = new MemoryStream(); 
       Rectangle origRect = new Rectangle(0, 0, originalImage.Width, originalImage.Height); 

       // if targets are null, require scale. for specified sized images eg. Event Image, or Profile photos. 
       int newWidth = 0; 
       int newHeight = 0; 

       //if the width is greater than height 
       if (originalImage.Width > originalImage.Height) 
       { 
        newWidth = targeWidth; 
        newHeight = targetHeight; 
        bitmap = new Bitmap(newWidth, newHeight); 
       } 

       //if the size of image is larger than either one of the target size 
       else if (originalImage.Width > targeWidth || originalImage.Height > targetHeight) 
       { 
        newWidth = targeWidth; 
        newHeight = targetHeight; 
        bitmap = new Bitmap(newWidth, newHeight); 
       } 
       //reuse old dimensions 
       else 
       { 
        bitmap = new Bitmap(originalImage.Width, originalImage.Height); 
       } 

       try 
       { 
        using (Graphics g = Graphics.FromImage((Image)bitmap)) 
        { 
         g.SmoothingMode = SmoothingMode.AntiAlias; 
         g.PixelOffsetMode = PixelOffsetMode.HighQuality; 
         g.CompositingQuality = CompositingQuality.HighQuality; 
         g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
         g.DrawImage(originalImage, new Rectangle(0, 0, newWidth, newHeight), 
          0, 
          0,   // upper-left corner of source rectangle 
          originalImage.Width,  // width of source rectangle 
          originalImage.Height,  // height of source rectangle 
          GraphicsUnit.Pixel); 
         bitmap.Save(newImage, ImageFormat.Jpeg); 
        } 
       } 
       catch (Exception ex) 
       { // error before IDisposable ownership transfer 
        if (bitmap != null) 
         bitmap.Dispose(); 

        logger.Info("Domain/Utilities/ImageResizer->ResizeFile: " + ex.ToString()); 

        throw new Exception("Error resizing file."); 
       } 
      }     
     } 

     return (Image)bitmap; 
    } 

UPDATE 1個 除去參數和編碼器,目前節約爲jpeg。但仍會產生粒狀圖像。

回答

0

一些建議

1)確保你是不是擴大圖像。

2)嘗試g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias代替高質量

未按上述情況,我會從上面刪除所有其他選項設置一旁,然後只需保存使用文件;

bitmap.Save(newImage, Imaging.ImageFormat.Jpeg); 

我發現上述作品非常好。

+0

1)不,我實際上是通過參數縮小到100x100px或27x27px 我需要以原始格式保存圖像。 * .png,* .gif等 –

+1

這不是你在你的代碼中做什麼。 imageEncoders [1]是jpeg –

+0

我更新了上面的代碼,刪除了編碼器等,並嘗試保存在.jpeg中。但它仍然會產生顆粒感的圖像。 –

相關問題