/// <summary> /// 按指定的壓縮質量及格式保存圖片(微軟的Image.Save方法保存到圖片壓縮質量爲75) /// </summary> /// <param name="sourceImage">要保存的圖片的Image對象</param> /// <param name="savePath">圖片要保存的絕對路徑</param> /// <param name="imageQualityValue">圖片要保存的壓縮質量,該參數的值爲1至100的整數,數值越大,保存質量越好</param> /// <returns>保存成功,返回true;反之,返回false</returns> public bool SaveImageForSpecifiedQuality(System.Drawing.Image sourceImage, string savePath, int imageQualityValue) { //如下代碼爲保存圖片時,設置壓縮質量 EncoderParameters encoderParameters = new EncoderParameters(); EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, imageQualityValue); encoderParameters.Param[0] = encoderParameter; try { ImageCodecInfo[] ImageCodecInfoArray = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegImageCodecInfo = null; for (int i = 0; i < ImageCodecInfoArray.Length; i++) { if (ImageCodecInfoArray[i].FormatDescription.Equals("JPEG")) { jpegImageCodecInfo = ImageCodecInfoArray[i]; break; } } sourceImage.Save(savePath, jpegImageCodecInfo, encoderParameters); return true; } catch { return false; } }