原本寬度為300 的圖片ImagePhoto 大小為 30KB
FixedSize(ImagePhoto,300) 之后 圖片 變?yōu)?20KB 了,圖片質(zhì)量下降了很多
怎么弄讓圖片質(zhì)量和原來的一樣呢?
public static Image FixedSize(Image imgPhoto, int Width) { int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int w, h; if (sourceWidth > Width) { decimal nPercent = (decimal)sourceWidth / Width; w = Width; h = (int)Math.Round((decimal)sourceHeight / nPercent, 0); } else { w = sourceWidth; h = sourceHeight; } Bitmap bmPhoto = new Bitmap(w, h, imgPhoto.PixelFormat); bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.CompositingQuality = CompositingQuality.HighQuality; grPhoto.SmoothingMode = SmoothingMode.HighQuality; grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, w, h), new Rectangle(0, 0, sourceWidth, sourceHeight), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; }
1 回答

斯蒂芬大帝
TA貢獻1827條經(jīng)驗 獲得超8個贊
如果是JPG的話,參考以下代碼:
var imageCodeInfo = GetEncoderInfo("image/jpeg");
var myEncoder = System.Drawing.Imaging.Encoder.Quality;
var myEncoderParameters = new EncoderParameters(1);
var myEncoderParameters.Param[0] = new EncoderParameter(myEncoder, 99L);
thumbNail.Save(thumbUrl, imageCodeInfo, myEncoderParameters);
- 1 回答
- 0 關注
- 1191 瀏覽
添加回答
舉報
0/150
提交
取消