代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
用于图片切割,图片压缩,缩略图的生成
作者:
Dezai.CN
/ 发布于
2012/3/6
/
685
<div> /// <summary></div> <div> /// 图片切割函数</div> <div> /// </summary></div> <div> /// <param name="sourceFile">原始图片文件</param></div> <div> /// <param name="xNum">在X轴上的切割数量</param></div> <div> /// <param name="yNum">在Y轴上的切割数量</param></div> <div> /// <param name="quality">质量压缩比</param></div> <div> /// <param name="outputFile">输出文件名,不带后缀</param></div> <div> /// <returns>成功返回true,失败则返回false</returns></div> <div> public static bool imageCut(String sourceFile, int xNum, int yNum, long quality, String outputFile)</div> <div> {</div> <div> try</div> <div> {</div> <div> long imageQuality = quality;</div> <div> Bitmap sourceImage = new Bitmap(sourceFile);</div> <div> ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");</div> <div> Encoder myEncoder = Encoder.Quality;</div> <div> EncoderParameters myEncoderParameters = new EncoderParameters(1);</div> <div> EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);</div> <div> myEncoderParameters.Param[0] = myEncoderParameter;</div> <div> float xWidth = sourceImage.Width / xNum;</div> <div> float yWidth = sourceImage.Height / yNum;</div> <div> String outputImage = "";</div> <div> </div> <div> for (int countY = 0; countY < yNum; countY++)</div> <div> for (int countX = 0; countX < xNum; countX++)</div> <div> {</div> <div></div> <div> RectangleF cloneRect = new RectangleF(countX * xWidth, countY * yWidth, xWidth, yWidth);</div> <div> Bitmap newImage = sourceImage.Clone(cloneRect, PixelFormat.Format24bppRgb);</div> <div> outputImage = outputFile + countX + countY + ".jpg";</div> <div> newImage.Save(outputImage, myImageCodecInfo, myEncoderParameters);</div> <div></div> <div> } <div> return true;</div> <div> } <div> catch</div> <div> {</div> <div> return false;</div> <div> } <div></div> <div> } <div> #endregion imageCut</div> <div></div> <div> #region imageCompress</div> <div> /// <summary></div> <div> /// 图片压缩函数</div> <div> /// </summary></div> <div> /// <param name="sourceFile">原始图片文件</param></div> <div> /// <param name="quality">质量压缩比</param></div> <div> /// <param name="ouputFile">输出文件名,请用 .jpg 后缀 </param></div> <div> /// <returns>成功返回true,失败则返回false</returns></div> <div> public static bool imageCompress(String sourceFile,long quality,String outputFile)</div> <div> {</div> <div> try</div> <div> {</div> <div> long imageQuality = quality;</div> <div> Bitmap sourceImage = new Bitmap(sourceFile);</div> <div> ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");</div> <div> Encoder myEncoder = Encoder.Quality;</div> <div> EncoderParameters myEncoderParameters = new EncoderParameters(1);</div> <div> EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);</div> <div> myEncoderParameters.Param[0] = myEncoderParameter;</div> <div> </div> <div> sourceImage.Save(outputFile, myImageCodecInfo, myEncoderParameters);</div> <div> return true;</div> <div></div> <div> } <div> catch</div> <div> {</div> <div> return false;</div> <div> } <div></div> <div> } <div> #endregion imageCompress</div> <div></div> <div> #region getThumImage</div> <div> /// <summary></div> <div> /// 生成缩略图</div> <div> /// </summary></div> <div> /// <param name="sourceFile">原始图片文件</param></div> <div> /// <param name="quality">质量压缩比</param></div> <div> /// <param name="multiple">收缩倍数</param></div> <div> /// <param name="outputFile">输出文件名</param></div> <div> /// <returns>成功返回true,失败则返回false</returns></div> <div> public static bool getThumImage(String sourceFile, long quality, int multiple, String outputFile)</div> <div> {</div> <div> try</div> <div> {</div> <div> long imageQuality = quality;</div> <div> Bitmap sourceImage = new Bitmap(sourceFile);</div> <div> ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");</div> <div> Encoder myEncoder = Encoder.Quality;</div> <div> EncoderParameters myEncoderParameters = new EncoderParameters(1);</div> <div> EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);</div> <div> myEncoderParameters.Param[0] = myEncoderParameter;</div> <div> float xWidth = sourceImage.Width;</div> <div> float yWidth = sourceImage.Height;</div> <div> Bitmap newImage = new Bitmap((int)(xWidth /multiple), (int)(yWidth /multiple));</div> <div> Graphics g = Graphics.FromImage(newImage);</div> <div> </div> <div> g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple);</div> <div> g.Dispose();</div> <div> newImage.Save(outputFile,myImageCodecInfo,myEncoderParameters);</div> <div> return true;</div> <div> } <div> catch</div> <div> {</div> <div> return false;</div> <div> } <div> } <div> #endregion getThumImage</div> <div></div> <div> #region ImageCodecInfo</div> <div> /// <summary></div> <div> /// 获取图片编码信息</div> <div> /// </summary></div> <div> private static ImageCodecInfo GetEncoderInfo(String mimeType)</div> <div> {</div> <div> int j;</div> <div> ImageCodecInfo[] encoders;</div> <div> encoders = ImageCodecInfo.GetImageEncoders();</div> <div> for (j = 0; j < encoders.Length; ++j)</div> <div> {</div> <div> if (encoders[j].MimeType == mimeType)</div> <div> return encoders[j];</div> <div> } <div> return null;</div> <div> }
试试其它关键字
图片切割
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3