/// <summary> /// 将图片大小标准化(4M限制) /// </summary> /// <param name="image"></param> /// <param name="width">压缩目标宽,会按原比例缩放</param> /// <param name="height">压缩目标高,会按原比例缩放</param> /// <returns></returns> public static Bitmap miniSizeImage(System.Drawing.Image image, int width, int height) { int _height = 0; int _width = 0; //height much bigger , so height is the max number of size if (image.Size.Height > image.Size.Width) { _height = height; _width = (int)((double)image.Size.Width / image.Size.Height * height); } else { _height = (int)((double)image.Size.Height / image.Size.Width * width); _width = width; }; ResizeBicubic filter = new ResizeBicubic(_width, _height); // apply the filter try { return(filter.Apply(image as Bitmap)); } catch (Exception ex) { return(image as Bitmap); } }
/// <summary> /// 将图片大小标准化(4M限制) /// </summary> /// <param name="stream"></param> /// <param name="width">压缩目标宽,会按原比例缩放</param> /// <param name="height">压缩目标高,会按原比例缩放</param> /// <param name="maxSize">压缩目标尺寸kb</param>/// /// <returns></returns> public static Bitmap miniSizeImage(FileStream stream, int width, int height, int maxSize = 2 * 1024, bool forceMin = false) { //System.Drawing.Image image = System.Drawing.Image.FromStream(stream); //if (!forceMin && stream.Length <= maxSize) //{ // return image as Bitmap; //} //var length = new FileInfo(image).Length; //return miniSizeImage(image, width, height); Stream res = CompressImage(stream, 90, maxSize, true); System.Drawing.Image image = System.Drawing.Image.FromStream(res); return(image as Bitmap); }
/// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片steam</param> /// <param name="dFile">压缩后保存图片地址</param> /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param> /// <param name="size">压缩后图片的最大大小kb</param> /// <param name="sfsc">是否是第一次调用</param> /// <returns></returns> public static Stream CompressImage(Stream sFile, int flag = 90, int size = 300, bool sfsc = true, Stream dFile = null) { //Console.WriteLine("sfile:" + sFile.Length); //如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true if (sfsc == true && sFile.Length < size * 1024) { //sFile.CopyTo(dFile); return(sFile); } System.Drawing.Image iSource = System.Drawing.Image.FromStream(sFile); ImageFormat tFormat = iSource.RawFormat; int dHeight = iSource.Height / 2; int dWidth = iSource.Width / 2; int sW = 0, sH = 0; //按比例缩放 Size tem_size = new Size(iSource.Width, iSource.Height); if (tem_size.Width > dHeight || tem_size.Width > dWidth) { if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth)) { sW = dWidth; sH = (dWidth * tem_size.Height) / tem_size.Width; } else { sH = dHeight; sW = (tem_size.Width * dHeight) / tem_size.Height; } } else { sW = tem_size.Width; sH = tem_size.Height; } Bitmap ob = new Bitmap(dWidth, dHeight); Graphics g = Graphics.FromImage(ob); g.Clear(Color.WhiteSmoke); g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel); g.Dispose(); //以下代码为保存图片时,设置压缩质量 EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = flag;//设置压缩的比例1-100 EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; try { if (dFile != null) { dFile.Close(); } dFile = new MemoryStream(); ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[x]; break; } } if (jpegICIinfo != null) { //ob.Save(fuckPath, jpegICIinfo, ep);//dFile是压缩后的 ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径 //Console.WriteLine("dfile:" + dFile.Length); //Console.WriteLine("dfile byte:"+ (dFile as MemoryStream).ToArray().LongLength); if (dFile.Length > 1024 * size) { flag -= 10; if (flag >= 0) { dFile = CompressImage(sFile, flag, size, false, dFile);//递归压缩直到可行 } else if (flag >= -100) { do { dFile = new MemoryStream(); var _radtio = (flag + 100) / 100.0; System.Drawing.Image image = System.Drawing.Image.FromStream(sFile); miniSizeImage(image, (int)(iSource.Width * _radtio), (int)(iSource.Height * _radtio)).Save(dFile, tFormat); flag -= 10; } while (dFile.Length > 1024 * size && flag >= -100); } else { dFile = sFile; } } } // return(dFile); } catch { return(sFile); } finally { iSource.Dispose(); ob.Dispose(); } }