C#压缩图片——高质量压缩方式
传入Bitmap对象,以及新图片的长宽(Bitmap.Size),这样生成的就是跟原图尺寸一致的低质量图片
public Bitmap GetImageThumb(Bitmap mg, Size newSize) { double ratio = 0d; double myThumbWidth = 0d; double myThumbHeight = 0d; int x = 0; int y = 0; Bitmap bp; if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height / Convert.ToDouble(newSize.Height))) ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width); else ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height); myThumbHeight = Math.Ceiling(mg.Height / ratio); myThumbWidth = Math.Ceiling(mg.Width / ratio); Size thumbSize = new Size((int)newSize.Width, (int)newSize.Height); bp = new Bitmap(newSize.Width, newSize.Height); x = (newSize.Width - thumbSize.Width) / 2; y = (newSize.Height - thumbSize.Height); System.Drawing.Graphics g = Graphics.FromImage(bp); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height); g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel); return bp; }