批量图片压缩工具
针对移动端网络图片加载缓慢,网上又缺少批量压缩图片的工具,于是自己开发了一个,分享出来,希望能给大家带来帮助。
工具:vs2017
创建项目 windowsform
文件压缩方法
/// <summary> /// 获取缩略图 /// </summary> /// <param name="bitmap"></param> /// <param name="width"></param> /// <returns></returns> public static Image GetThumbnailImageByte(Image bitmap, int width) { System.Drawing.Image img = bitmap.Clone() as Image; if (bitmap.RawFormat == System.Drawing.Imaging.ImageFormat.Gif) { #region gif动态图缩放 //不够100*100的不缩放 if (img.Width > width) { System.IO.MemoryStream stream = new System.IO.MemoryStream(); var height = (img.Height * width) / img.Width; //新图第一帧 Image new_img = new Bitmap(width, height); //新图其他帧 Image new_imgs = new Bitmap(width, height); //新图第一帧GDI+绘图对象 Graphics g_new_img = Graphics.FromImage(new_img); //新图其他帧GDI+绘图对象 Graphics g_new_imgs = Graphics.FromImage(new_imgs); //配置新图第一帧GDI+绘图对象 g_new_img.CompositingMode = CompositingMode.SourceCopy; g_new_img.InterpolationMode = InterpolationMode.HighQualityBicubic; g_new_img.PixelOffsetMode = PixelOffsetMode.HighQuality; g_new_img.SmoothingMode = SmoothingMode.HighQuality; g_new_img.Clear(Color.FromKnownColor(KnownColor.Transparent)); //配置其他帧GDI+绘图对象 g_new_imgs.CompositingMode = CompositingMode.SourceCopy; g_new_imgs.InterpolationMode = InterpolationMode.HighQualityBicubic; g_new_imgs.PixelOffsetMode = PixelOffsetMode.HighQuality; g_new_imgs.SmoothingMode = SmoothingMode.HighQuality; g_new_imgs.Clear(Color.FromKnownColor(KnownColor.Transparent)); //遍历维数 foreach (Guid gid in img.FrameDimensionsList) { //因为是缩小GIF文件所以这里要设置为Time //如果是TIFF这里要设置为PAGE FrameDimension f = FrameDimension.Time; //获取总帧数 int count = img.GetFrameCount(f); //保存标示参数 System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.SaveFlag; // EncoderParameters ep = null; //图片编码、解码器 ImageCodecInfo ici = null; //图片编码、解码器集合 ImageCodecInfo[] icis = ImageCodecInfo.GetImageDecoders(); //为 图片编码、解码器 对象 赋值 foreach (ImageCodecInfo ic in icis) { if (ic.FormatID == ImageFormat.Gif.Guid) { ici = ic; break; } } //每一帧 for (int c = 0; c < count; c++) { //选择由维度和索引指定的帧 img.SelectActiveFrame(f, c); //第一帧 if (c == 0) { //将原图第一帧画给新图第一帧 g_new_img.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); //把振频和透明背景调色板等设置复制给新图第一帧 for (int i = 0; i < img.PropertyItems.Length; i++) { new_img.SetPropertyItem(img.PropertyItems[i]); } ep = new EncoderParameters(1); //第一帧需要设置为MultiFrame ep.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.MultiFrame); //保存第一帧 new_img.Save(stream, ici, ep); } //其他帧 else { //把原图的其他帧画给新图的其他帧 g_new_imgs.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); //把振频和透明背景调色板等设置复制给新图第一帧 for (int i = 0; i < img.PropertyItems.Length; i++) { new_imgs.SetPropertyItem(img.PropertyItems[i]); } ep = new EncoderParameters(1); //如果是GIF这里设置为FrameDimensionTime //如果为TIFF则设置为FrameDimensionPage ep.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.FrameDimensionTime); //向新图添加一帧 new_img.SaveAdd(new_imgs, ep); } } ep = new EncoderParameters(1); //关闭多帧文件流 ep.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.Flush); new_img.SaveAdd(ep); } byte[] byData = new Byte[stream.Length]; stream.Position = 0; stream.Read(byData, 0, byData.Length); stream.Close(); //释放文件 img.Dispose(); new_img.Dispose(); new_imgs.Dispose(); g_new_img.Dispose(); g_new_imgs.Dispose(); return BytesToBitmap(byData); } #endregion } else { #region 静态缩略图 if (img.Width > width) { Graphics g = null; try { var height = (img.Height * width) / img.Width; Bitmap bmpDest = new Bitmap(width, height); g = Graphics.FromImage(bmpDest); //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default; g.Clear(Color.White); g.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, img.Size.Width, img.Size.Height), GraphicsUnit.Pixel); g.Flush(); return bmpDest; } catch { return null; } finally { g.Dispose(); img.Dispose(); } } #endregion } return img; }
工具下载地址:
链接:https://pan.baidu.com/s/19MTOQbssM7Dbzwwc5E1HLw
提取码:xw2x
源码下载地址:
链接:https://pan.baidu.com/s/1Txez9qzBFQXauiAIazlCpw
提取码:gyze