/// <summary> /// 将图片按百分比压缩 /// </summary> public static bool ImageCompress(Image source, string savePath, byte percent) { var parameters = new EncoderParameters(); parameters.Param[0] = new EncoderParameter(Encoder.Quality, new long[] { percent }); try { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders(); ImageCodecInfo jpegICIinfo = arrayICI.SingleOrDefault(a => a.FormatDescription.Equals("JPEG", StringComparison.OrdinalIgnoreCase)); if (jpegICIinfo != null) source.Save(savePath, jpegICIinfo, parameters); else source.Save(savePath, source.RawFormat); return true; } catch (Exception ex) { return false; } finally { source.Dispose(); } } public static Bitmap PercentImage(Image srcImage) { int newW = srcImage.Width < 1130 ? srcImage.Width : 1130; int newH = int.Parse(Math.Round(srcImage.Height * (double)newW / srcImage.Width).ToString()); try { Bitmap b = new Bitmap(newW, newH); Graphics g = Graphics.FromImage(b); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default; g.DrawImage(srcImage, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel); g.Dispose(); return b; } catch (Exception) { return null; } }