C#图片切割、图片压缩、缩略图生成代码汇总
来源:本站原创|时间:2020-01-10|栏目:C#教程|点击: 次
本文为大家整理了C#图片切割、图片压缩、缩略图生成的实现代码,大家可以收藏,方便以后使用,具体内容如下
/// 图片切割函数 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="xNum">在X轴上的切割数量</param> /// <param name="yNum">在Y轴上的切割数量</param> /// <param name="quality">质量压缩比</param> /// <param name="outputFile">输出文件名,不带后缀</param> /// <returns>成功返回true,失败则返回false</returns> public static bool imageCut(String sourceFile, int xNum, int yNum, long quality, String outputFile) { try { long imageQuality = quality; Bitmap sourceImage = new Bitmap(sourceFile); ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); Encoder myEncoder = Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); myEncoderParameters.Param[0] = myEncoderParameter; float xWidth = sourceImage.Width / xNum; float yWidth = sourceImage.Height / yNum; String outputImage = ""; for (int countY = 0; countY < yNum; countY++) for (int countX = 0; countX < xNum; countX++) { RectangleF cloneRect = new RectangleF(countX * xWidth, countY * yWidth, xWidth, yWidth); Bitmap newImage = sourceImage.Clone(cloneRect, PixelFormat.Format24bppRgb); outputImage = outputFile + countX + countY + ".jpg"; newImage.Save(outputImage, myImageCodecInfo, myEncoderParameters); } return true; } catch { return false; } } #endregion imageCut imageCompress #region imageCompress /**//// <summary> /// 图片压缩函数 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="quality">质量压缩比</param> /// <param name="ouputFile">输出文件名,请用 .jpg 后缀 </param> /// <returns>成功返回true,失败则返回false</returns> public static bool imageCompress(String sourceFile,long quality,String outputFile) { try { long imageQuality = quality; Bitmap sourceImage = new Bitmap(sourceFile); ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); Encoder myEncoder = Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); myEncoderParameters.Param[0] = myEncoderParameter; sourceImage.Save(outputFile, myImageCodecInfo, myEncoderParameters); return true; } catch { return false; } } #endregion imageCompress getThumImage#region getThumImage /**//// <summary> /// 生成缩略图 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="quality">质量压缩比</param> /// <param name="multiple">收缩倍数</param> /// <param name="outputFile">输出文件名</param> /// <returns>成功返回true,失败则返回false</returns> public static bool getThumImage(String sourceFile, long quality, int multiple, String outputFile) { try { long imageQuality = quality; Bitmap sourceImage = new Bitmap(sourceFile); ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); Encoder myEncoder = Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); myEncoderParameters.Param[0] = myEncoderParameter; float xWidth = sourceImage.Width; float yWidth = sourceImage.Height; Bitmap newImage = new Bitmap((int)(xWidth /multiple), (int)(yWidth /multiple)); Graphics g = Graphics.FromImage(newImage); g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple); g.Dispose(); newImage.Save(outputFile,myImageCodecInfo,myEncoderParameters); return true; } catch { return false; } } #endregion getThumImage ImageCodecInfo#region ImageCodecInfo /**//// <summary> /// 获取图片编码信息 /// </summary> private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for (j = 0; j < encoders.Length; ++j) { if (encoders[j].MimeType == mimeType) return encoders[j]; } return null; }
以上就是本文的全部内容,希望对大家的学习有所帮助。
栏 目:C#教程
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/6531.html
您可能感兴趣的文章
- 01-10C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- 01-10C#编程获取资源文件中图片的方法
- 01-10C#禁止textbox复制、粘贴、剪切及鼠标右键的方法
- 01-10http图片上传安全性问题 根据ContentType (MIME) 判断其实不准确、不
- 01-10浅析JAVA中过滤器、监听器、拦截器的区别
- 01-10C#将图片存放到SQL SERVER数据库中的方法
- 01-10C#调用WebService实例开发
- 01-10C#编程自学之开篇介绍
- 01-10C#操作数据库中存取图片文件的方法
- 01-10C#图片处理3种高级应用
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10C#中split用法实例总结
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10delphi制作wav文件的方法
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11ajax实现页面的局部加载
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10SublimeText编译C开发环境设置
- 04-02jquery与jsp,用jquery