/// <summary> /// 保存图片 /// </summary> /// <param name="srcImage">要保存的图片</param> /// <param name="quality">品质(1L~100L之间,数值越大品质越好)</param> /// <param name="filename">保存路径</param> public static void SaveImage(Image srcImage, long quality, string filename) { using (EncoderParameters encoderParams = new EncoderParameters(1)) { encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality); ImageCodecInfo encoder = null; string ext = Path.GetExtension(filename); if (string.IsNullOrEmpty(ext)) { ext = ".jpg"; } //根据扩展名得到解码、编码器 foreach (ImageCodecInfo codecInfo in ImageCodecInfo.GetImageEncoders()) { if (Regex.IsMatch(codecInfo.FilenameExtension, string.Format(@"(;|^)\*\{0}(;|$)", ext), RegexOptions.IgnoreCase)) { encoder = codecInfo; break; } } Directory.CreateDirectory(Path.GetDirectoryName(filename)); srcImage.Save(filename, encoder, encoderParams); } }