using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Pcb.Common.Utilities { public class ImageUtility { public static void CompressionAndSaveImage(System.Web.HttpPostedFile file, string savePath, int width = 640, int height = 360, int Level = 80, bool CompressionAlways = false) { if (Level < 0 || Level > 100) throw new Exception("压缩率必须在0-100之间"); // Get a bitmap. if (file.ContentLength <= 30 * 1024 && !CompressionAlways) { file.SaveAs(savePath); return; } Image bmpOld = Image.FromStream(file.InputStream); Bitmap img = new Bitmap(bmpOld); Bitmap bmp2 = null; bmpOld.Dispose(); if (file.ContentLength > 100 * 1024 || CompressionAlways) { int srcWidth = img.Width; int srcHeight = img.Height; int thumbWidth = width; int thumbHeight = height; ImageWH iwh = GetImageNewWH(srcWidth, srcHeight, width, height); thumbWidth = iwh.width; thumbHeight = iwh.height; if (thumbWidth > width || thumbHeight > height) { iwh = GetImageNewWH(thumbWidth, thumbHeight, width, height); thumbWidth = iwh.width; thumbHeight = iwh.height; } Bitmap bmp = new Bitmap(thumbWidth, thumbHeight); //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。 System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp); //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //gr.SmoothingMode = SmoothingMode.None; ////下面这个也设成高质量 gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //gr.CompositingQuality = CompositingQuality.HighSpeed; ////下面这个设成High gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //gr.InterpolationMode = InterpolationMode.Low; gr.Clear(System.Drawing.Color.Transparent); img.SetResolution(72, 72); //把原始图像绘制成上面所设置宽高的缩小图 System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight); gr.DrawImage(img, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel); bmp2 = new Bitmap(bmp); bmp.Dispose(); gr.Dispose(); } else bmp2 = img; ImageCodecInfo Encoder = null; Encoder = GetEncoder(ImageFormat.Jpeg); // Create an Encoder object based on the GUID // for the Quality parameter category. System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; // Create an EncoderParameters object. // An EncoderParameters object has an array of EncoderParameter // objects. In this case, there is only one // EncoderParameter object in the array. EncoderParameters EncoderParameters = new EncoderParameters(1); EncoderParameter EncoderParameter = new EncoderParameter(myEncoder, Level); EncoderParameters.Param[0] = EncoderParameter; bmp2.Save(savePath, Encoder, EncoderParameters); bmp2.Dispose(); img.Dispose(); } private static ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) return codec; } return null; } private static ImageWH GetImageNewWH(int srcWidth, int srcHeight, int width, int height) { ImageWH iwh = new ImageWH(); if (srcWidth > width && srcHeight > height) { if (srcWidth > srcHeight) { double heightFloat = Convert.ToDouble(srcHeight) / Convert.ToDouble(srcWidth); heightFloat = heightFloat * width; iwh.height = Convert.ToInt32(heightFloat); iwh.width = width; } else { double widthFloat = Convert.ToDouble(srcWidth) / Convert.ToDouble(srcHeight); widthFloat = widthFloat * height; iwh.width = Convert.ToInt32(widthFloat); iwh.height = height; } } else if (srcWidth > width) { double heightFloat = Convert.ToDouble(srcHeight) / Convert.ToDouble(srcWidth); heightFloat = heightFloat * width; iwh.height = Convert.ToInt32(heightFloat); iwh.width = width; } else if (srcHeight > height) { double widthFloat = Convert.ToDouble(srcWidth) / Convert.ToDouble(srcHeight); widthFloat = widthFloat * height; iwh.width = Convert.ToInt32(widthFloat); iwh.height = height; } else { iwh.width = srcWidth; iwh.height = srcHeight; } return iwh; } private class ImageWH { public int width { get; set; } public int height { get; set; } } } }