2011-01-11 40 views
0

我正在開發c#語言的asp.net MVC應用程序,在那裏我提供了上傳圖像的工具。我想提供裁剪圖像並顯示其縮略圖。我通過使用System.Drawing.Bitmap命名空間及其類來實現這一點,但它在壓縮時會影響縮略圖的質量。我必須做的。我經歷了這個link如何編寫c#中的質量縮略圖圖像

但不能趕上很多。請幫忙。 編輯: 我試過之前應用上面的鏈接:和我的代碼是:

公共靜態無效ResizeAndSaveHighQualityImage(爲System.Drawing.Image形象,詮釋寬度,高度INT,串pathToSave,INT質量) {

// the resized result bitmap 

    using (Bitmap result = new Bitmap(width, height)) 
    { 

     // get the graphics and draw the passed image to the result bitmap 

     using (Graphics grphs = Graphics.FromImage(result)) 
     { 

      grphs.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 

      grphs.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 

      grphs.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 

      grphs.DrawImage(image, 0, 0, result.Width, result.Height); 

     } 
     // check the quality passed in 

     if ((quality < 0) || (quality > 100)) 
     { 

      string error = string.Format("quality must be 0, 100", quality); 

      throw new ArgumentOutOfRangeException(error); 

     } 



     EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); 

     string lookupKey = "image/jpeg"; 

     var jpegCodec = ImageCodecInfo.GetImageEncoders().Where(i => i.MimeType.Equals(lookupKey)).FirstOrDefault(); 



     //create a collection of EncoderParameters and set the quality parameter 

     var encoderParams = new EncoderParameters(1); 

     encoderParams.Param[0] = qualityParam; 

     //save the image using the codec and the encoder parameter 

     result.Save(pathToSave, jpegCodec, encoderParams); 

    } 

} 

這也會產生低質量圖像。我也無法從給定的鏈接上獲得太多。爲什麼我需要在那裏寫處理程序?這是必要的嗎?

回答

1

也許問題是你沒有設置你想要的壓縮格式。

您是否使用過JPG或PNG格式?

檢查Bitmap.Save重載,你會發現一些需要的編碼器參數,它可以讓你提供一個MIME類型,質量等級。

我希望這是有用的:)

+0

感謝您的回覆。請檢查問題,我已編輯並在那裏添加我的代碼 – 2011-01-11 10:27:02