原本使用Image.GetThumbnailImage來做縮圖,結果畫質相當差,模糊不清,所以改用Graphics重繪
Graphics物件重繪圖片的畫質會比Image.GetThumbnailImage來的好
以下為參考程式碼
//讀取圖檔 Image inputImage = Image.FromStream(new MemoryStream(File.ReadAllBytes("圖檔來源"))); //縮圖,此處縮圖大小為800x600 Bitmap thumbnailBitmap = new Bitmap(800, 600); Graphics thumbnailGraph = Graphics.FromImage(thumbnailBitmap); //這三個屬性設定圖片品質 thumbnailGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; thumbnailGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; thumbnailGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //重繪圖片 Rectangle imageRectangle = new Rectangle(0, 0, 800, 600); thumbnailGraph.DrawImage(inputImage, imageRectangle); //存檔 thumbnailBitmap.Save(savePath + Path.GetFileName(file), inputImage.RawFormat); //釋放資源 thumbnailGraph.Dispose(); thumbnailBitmap.Dispose(); inputImage.Dispose(); GC.Collect();
參考資源:
1.
MSDN
2.http://sweeteason.pixnet.net/blog/post/31925002-%E8%87%AA%E5%B7%B1%E7%AD%86%E8%A8%98---asp.net-%E5%9C%96%E7%89%87%E5%8B%95%E6%85%8B%E7%B8%AE%E5%9C%96-(%E9%AB%98%E7%95%AB%E8%B3%AA%E7%BA%8C%E7%AF%87)
文章標籤
全站熱搜
留言列表