日期:2014-05-18 浏览次数:21014 次
#region 图片压缩,转换为JPG格式 /// <summary> /// 获取指定mimeType的ImageCodecInfo /// </summary> private static ImageCodecInfo GetImageCodecInfo(string mimeType) { ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders(); foreach(ImageCodecInfo ici in CodecInfo) { if(ici.MimeType == mimeType) return ici; } return null; } /// <summary> /// 获取inputStream中的Bitmap对象 /// </summary> public static Bitmap GetBitmapFromStream(Stream inputStream) { Bitmap bitmap = new Bitmap(inputStream); return bitmap; } /// <summary> /// 将Bitmap对象压缩为JPG图片类型 /// </summary> /// <param name="bmp">源bitmap对象</param> /// <param name="saveFilePath">目标图片的存储地址</param> /// <param name="quality">压缩质量,越大照片越清晰,推荐80</param> public static void CompressAsJPG(Bitmap bmp, string saveFilePath, int quality) { EncoderParameter p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); ; EncoderParameters ps = new EncoderParameters(1); ps.Param[0] = p; bmp.Save(saveFilePath, GetImageCodecInfo("image/jpeg"), ps); bmp.Dispose(); } /// <summary> /// 将inputStream中的对象压缩为JPG图片类型 /// </summary> /// <param name="inputStream">源Stream对象</param> /// <param name="saveFilePath">目标图片的存储地址</param> /// <param name="quality">压缩质量,越大照片越清晰,推荐80</param> public static void CompressAsJPG(Stream inputStream, string saveFilePath, int quality) { Bitmap bmp = GetBitmapFromStream(inputStream); CompressAsJPG(bmp, saveFilePath, quality); } #endregion