C#无损压缩图片
话不多说,请看代码:
/// <summary> /// 根据指定尺寸得到按比例缩放的尺寸,返回true表示以更改尺寸 /// </summary> /// <param name="picWidth">图片宽度</param> /// <param name="picHeight">图片高度</param> /// <param name="specifiedWidth">指定宽度</param> /// /// <param name="specifiedHeight">指定高度</param> /// <returns>返回true表示以更改尺寸</returns> private bool GetPicZoomSize(ref int picWidth, ref int picHeight, int specifiedWidth, int specifiedHeight) { int sW = 0, sH = 0; Boolean isZoomSize = false; //按比例缩放 Size tem_size = new Size(picWidth, picHeight); if (tem_size.Width > specifiedWidth || tem_size.Height > specifiedHeight) //将**改成c#中的或者操作符号 { if ((tem_size.Width * specifiedHeight) > (tem_size.Height * specifiedWidth)) { sW = specifiedWidth; sH = (specifiedWidth * tem_size.Height) / tem_size.Width; } else { sH = specifiedHeight; sW = (tem_size.Width * specifiedHeight) / tem_size.Height; } isZoomSize = true; } else { sW = tem_size.Width; sH = tem_size.Height; } picHeight = sH; picWidth = sW; return isZoomSize; } /// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片</param> /// <param name="dFile">压缩后保存位置</param> /// <param name="dHeight">高度</param> /// <param name="dWidth">宽度</param> /// <param name="flag">压缩质量 1-100</param> /// <returns></returns> public bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth, int flag) { System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile); ImageFormat tFormat = iSource.RawFormat; int sW = iSource.Width, sH = iSource.Height; GetPicZoomSize(ref sW, ref sH, dWidth, dHeight); Bitmap ob = new Bitmap(dWidth, dHeight); Graphics g = Graphics.FromImage(ob); g.Clear(Color.WhiteSmoke); g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 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 x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[x]; 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(); } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持我们!
上一篇:用Newtonsoft将json串转为对象的方法(详解)
栏 目:C#教程
下一篇:c#进程之间对象传递方法
本文标题:C#无损压缩图片
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5792.html
您可能感兴趣的文章
- 01-10C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- 01-10C#编程获取资源文件中图片的方法
- 01-10http图片上传安全性问题 根据ContentType (MIME) 判断其实不准确、不
- 01-10C#将图片存放到SQL SERVER数据库中的方法
- 01-10C#操作数据库中存取图片文件的方法
- 01-10C#图片处理3种高级应用
- 01-10c#实现识别图片上的验证码数字
- 01-10Silverlight将图片转换为byte的实现代码
- 01-10C#简易图片格式转换器实现方法
- 01-10C#给图片添加水印完整实例
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-10C#中split用法实例总结
- 04-02jquery与jsp,用jquery
- 01-10SublimeText编译C开发环境设置
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-11ajax实现页面的局部加载
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10delphi制作wav文件的方法
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05DEDE织梦data目录下的sessions文件夹有什