/// <summary> /// 获取无损压缩图片 /// </summary> /// <param name="sourceFilePath">原图片</param> /// <param name="thumFilePath">压缩后保存位置</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> /// <returns>压缩后的新路径</returns> public static string GetThumbnailImg(string sourceFilePath, string thumFilePath, int width, int height) { int flag = 100; Image soureImg = Image.FromFile(sourceFilePath); if (soureImg.Height < soureImg.Width) { soureImg = KiRotate(soureImg, RotateFlipType.Rotate90FlipNone); } ImgThumbnailType type = soureImg.Width > soureImg.Height ? ImgThumbnailType.W : ImgThumbnailType.H; string imgPath = GetThumbnailImg(soureImg, thumFilePath, width, height, flag, type, ImageFormat.Png); if (!string.IsNullOrWhiteSpace(imgPath)) { return(imgPath); } else { return(GetThumbnailImg(sourceFilePath, thumFilePath, width, height, ImageFormat.Png)); } }
/// <summary> /// 获取无损压缩图片 /// </summary> /// <param name="sFile">原图片</param> /// <param name="thumFilePath">压缩后保存位置</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> /// <param name="flag">压缩质量 1-100</param> /// <param name="type">压缩缩放类型</param> /// <returns>压缩后的新路径</returns> private static string GetThumbnailImg(string sourceImgPath, string thumFilePath, int width, int height, ImageFormat imageFormat) { int flag = 100; string fileName = Path.GetFileNameWithoutExtension(thumFilePath); thumFilePath = thumFilePath.Replace(fileName, string.Format("{0}_temp", fileName)); Image sourceImg = Image.FromFile(sourceImgPath); if (sourceImg.Height < sourceImg.Width) { sourceImg = KiRotate(sourceImg, RotateFlipType.Rotate270FlipNone); } ImgThumbnailType type = sourceImg.Width > sourceImg.Height ? ImgThumbnailType.W : ImgThumbnailType.H; sourceImg.Save(thumFilePath); sourceImg = Image.FromFile(thumFilePath); //缩放后的宽度和高度 int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = sourceImg.Width; int oh = sourceImg.Height; switch (type) { case ImgThumbnailType.WH: //指定高宽缩放(可能变形) { break; } case ImgThumbnailType.W: //指定宽,高按比例 { toheight = sourceImg.Height * width / sourceImg.Width; break; } case ImgThumbnailType.H: //指定高,宽按比例 { towidth = sourceImg.Width * height / sourceImg.Height; break; } case ImgThumbnailType.Cut: //指定高宽裁减(不变形) { if ((double)sourceImg.Width / (double)sourceImg.Height > (double)towidth / (double)toheight) { oh = sourceImg.Height; ow = sourceImg.Height * towidth / toheight; y = 0; x = (sourceImg.Width - ow) / 2; } else { ow = sourceImg.Width; oh = sourceImg.Width * height / towidth; x = 0; y = (sourceImg.Height - oh) / 2; } break; } default: break; } Bitmap ob = new Bitmap(towidth, toheight); try { using (Graphics g = Graphics.FromImage(ob)) { g.Clear(System.Drawing.Color.WhiteSmoke); g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(sourceImg , new Rectangle(x, y, towidth, toheight) , new Rectangle(0, 0, sourceImg.Width, sourceImg.Height) , GraphicsUnit.Pixel); g.Dispose(); } } catch (Exception ex) { LogUtil.Error(string.Format(@"图片文件压缩失败,参考信息:{0}。", ex.Message.ToString())); sourceImg.Dispose(); ob.Dispose(); return(string.Empty); } //以下代码为保存图片时,设置压缩质量 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 { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int i = 0; i < arrayICI.Length; i++) { if (arrayICI[i].FormatDescription.Equals(imageFormat.ToString(), StringComparison.CurrentCultureIgnoreCase)) { jpegICIinfo = arrayICI[i]; break; } } thumFilePath = thumFilePath.Replace(string.Format("{0}_temp", fileName), fileName); if (jpegICIinfo != null) { ob.Save(thumFilePath, jpegICIinfo, ep); } else { ob.Save(thumFilePath, imageFormat); } return(thumFilePath); } catch { return(string.Empty); } finally { sourceImg.Dispose(); ob.Dispose(); } }
/// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片</param> /// <param name="dFile">压缩后保存位置</param> /// <param name="height">高度</param> /// <param name="width"></param> /// <param name="flag">压缩质量 1-100</param> /// <param name="type">压缩缩放类型</param> /// <returns></returns> public Bitmap Thumbnail(Bitmap iSource, int height, int width, int flag) { ImgThumbnailType type = ImgThumbnail.ImgThumbnailType.H; //System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile); ImageFormat tFormat = iSource.RawFormat; //缩放后的宽度和高度 int towidth = width; int toheight = height; // int x = 0; int y = 0; int ow = iSource.Width; int oh = iSource.Height; switch (type) { case ImgThumbnailType.WH: //指定高宽缩放(可能变形) { break; } case ImgThumbnailType.W: //指定宽,高按比例 { toheight = iSource.Height * width / iSource.Width; break; } case ImgThumbnailType.H: //指定高,宽按比例 { towidth = iSource.Width * height / iSource.Height; break; } case ImgThumbnailType.Cut: //指定高宽裁减(不变形) { if ((double)iSource.Width / (double)iSource.Height > (double)towidth / (double)toheight) { oh = iSource.Height; ow = iSource.Height * towidth / toheight; y = 0; x = (iSource.Width - ow) / 2; } else { ow = iSource.Width; oh = iSource.Width * height / towidth; x = 0; y = (iSource.Height - oh) / 2; } break; } default: break; } Bitmap ob = new Bitmap(towidth, toheight); Graphics g = Graphics.FromImage(ob); g.Clear(System.Drawing.Color.WhiteSmoke); g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(iSource , new Rectangle(x, y, towidth, toheight) , new Rectangle(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 { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int i = 0; i < arrayICI.Length; i++) { if (arrayICI[i].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[i]; break; } } //if (jpegICIinfo != null) //{ // ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径 //} //else //{ // ob.Save(dFile, tFormat); //} return(ob); } catch { return(null); } finally { iSource.Dispose(); iSource = null; //ob.Dispose(); } }
/// <summary> /// 获取无损压缩图片 /// </summary> /// <param name="sourceFilePath">原图片</param> /// <param name="thumFilePath">压缩后保存位置</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> /// <param name="flag">压缩质量 1-100</param> /// <param name="type">压缩缩放类型</param> /// <returns>压缩后的新路径</returns> public static string GetThumbnailImg(string sourceFilePath, string thumFilePath, int width, int height, int flag, ImgThumbnailType type, ImageFormat imageFormat) { Image soureImg = Image.FromFile(sourceFilePath); if (soureImg.Height < soureImg.Width) { soureImg = KiRotate(soureImg, RotateFlipType.Rotate90FlipNone); } return(GetThumbnailImg(soureImg, thumFilePath, width, height, flag, type, imageFormat)); }
/// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片</param> /// <param name="dFile">压缩后保存位置</param> /// <param name="height">高度</param> /// <param name="width"></param> /// <param name="flag">压缩质量 1-100</param> /// <param name="type">压缩缩放类型</param> /// <returns></returns> public bool Thumbnail(string sFile, string dFile, int height, int width, int flag, ImgThumbnailType type) { System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile); ImageFormat tFormat = iSource.RawFormat; //缩放后的宽度和高度 int towidth = width; int toheight = height; // int x = 0; int y = 0; int ow = iSource.Width; int oh = iSource.Height; switch (type) { case ImgThumbnailType.WH://指定高宽缩放(可能变形) { break; } case ImgThumbnailType.W://指定宽,高按比例 { toheight = iSource.Height * width / iSource.Width; break; } case ImgThumbnailType.H://指定高,宽按比例 { towidth = iSource.Width * height / iSource.Height; break; } case ImgThumbnailType.Cut://指定高宽裁减(不变形) { if ((double)iSource.Width / (double)iSource.Height > (double)towidth / (double)toheight) { oh = iSource.Height; ow = iSource.Height * towidth / toheight; y = 0; x = (iSource.Width - ow) / 2; } else { ow = iSource.Width; oh = iSource.Width * height / towidth; x = 0; y = (iSource.Height - oh) / 2; } break; } default: break; } Bitmap ob = new Bitmap(towidth, toheight); Graphics g = Graphics.FromImage(ob); g.Clear(System.Drawing.Color.WhiteSmoke); g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(iSource , new Rectangle(x, y, towidth, toheight) , new Rectangle(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 { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int i = 0; i < arrayICI.Length; i++) { if (arrayICI[i].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[i]; break; } } if (jpegICIinfo != null) { ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径 } else { ob.Save(dFile, tFormat); } return true; } catch { return false; } finally { iSource.Dispose(); ob.Dispose(); } }