下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing; using System.Drawing.drawing2d; using System.Drawing.Imaging; namespace WuJian.Common { /// <summary> /// 图片处理 /// http://www.cnblogs.com/wu-jian/ /// /// 吴剑 2011-02-20 创建 /// 吴剑 2012-08-08 修改 /// </summary> public class Image { #region 正方型裁剪并缩放 /// <summary> /// 正方型裁剪 /// 以图片中心为轴心,截取正方型,然后等比缩放 /// 用于头像处理 /// </summary> /// <remarks>吴剑 2012-08-08</remarks> /// <param name="fromfile">原图Stream对象</param> /// <param name="fileSaveUrl">缩略图存放地址</param> /// <param name="sIDe">指定的边长(正方型)</param> /// <param name="quality">质量(范围0-100)</param> public static voID CutForSquare(System.IO.Stream fromfile,string fileSaveUrl,int sIDe,int quality) { //创建目录 string dir = Path.GetDirectoryname(fileSaveUrl); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息) System.Drawing.Image initimage = System.Drawing.Image.FromStream(fromfile,true); //原图宽高均小于模版,不作处理,直接保存 if (initimage.WIDth <= sIDe && initimage.Height <= sIDe) { initimage.Save(fileSaveUrl,System.Drawing.Imaging.ImageFormat.Jpeg); } else { //原始图片的宽、高 int initWIDth = initimage.WIDth; int initHeight = initimage.Height; //非正方型先裁剪为正方型 if (initWIDth != initHeight) { //截图对象 System.Drawing.Image pickedImage = null; system.drawing.graphics pickedG = null; //宽大于高的横图 if (initWIDth > initHeight) { //对象实例化 pickedImage = new System.Drawing.Bitmap(initHeight,initHeight); pickedG = system.drawing.graphics.FromImage(pickedImage); //设置质量 pickedG.InterpolationMode = System.Drawing.drawing2d.InterpolationMode.HighQualityBicubic; pickedG.SmoothingMode = System.Drawing.drawing2d.SmoothingMode.HighQuality; //定位 Rectangle fromr = new Rectangle((initWIDth - initHeight) / 2,initHeight,initHeight); Rectangle toR = new Rectangle(0,initHeight); //画图 pickedG.DrawImage(initimage,toR,fromr,system.drawing.graphicsUnit.Pixel); //重置宽 initWIDth = initHeight; } //高大于宽的竖图 else { //对象实例化 pickedImage = new System.Drawing.Bitmap(initWIDth,initWIDth); pickedG = system.drawing.graphics.FromImage(pickedImage); //设置质量 pickedG.InterpolationMode = System.Drawing.drawing2d.InterpolationMode.HighQualityBicubic; pickedG.SmoothingMode = System.Drawing.drawing2d.SmoothingMode.HighQuality; //定位 Rectangle fromr = new Rectangle(0,(initHeight - initWIDth) / 2,initWIDth,initWIDth); Rectangle toR = new Rectangle(0,initWIDth); //画图 pickedG.DrawImage(initimage,system.drawing.graphicsUnit.Pixel); //重置高 initHeight = initWIDth; } //将截图对象赋给原图 initimage = (System.Drawing.Image)pickedImage.Clone(); //释放截图资源 pickedG.dispose(); pickedImage.dispose(); } //缩略图对象 System.Drawing.Image resultimage = new System.Drawing.Bitmap(sIDe,sIDe); system.drawing.graphics resultG = system.drawing.graphics.FromImage(resultimage); //设置质量 resultG.InterpolationMode = System.Drawing.drawing2d.InterpolationMode.HighQualityBicubic; resultG.SmoothingMode = System.Drawing.drawing2d.SmoothingMode.HighQuality; //用指定背景色清空画布 resultG.Clear(color.White); //绘制缩略图 resultG.DrawImage(initimage,new System.Drawing.Rectangle(0,sIDe,sIDe),initHeight),system.drawing.graphicsUnit.Pixel); //关键质量控制 //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff ImageCodecInfo[] icis = ImageCodecInfo.GetimageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo i in icis) { if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif") { ici = i; } } EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,(long)quality); //保存缩略图 resultimage.Save(fileSaveUrl,ici,ep); //释放关键质量控制所用资源 ep.dispose(); //释放缩略图资源 resultG.dispose(); resultimage.dispose(); //释放原始图片资源 initimage.dispose(); } } #endregion #region 自定义裁剪并缩放 /// <summary> /// 指定长宽裁剪 /// 按模版比例最大范围的裁剪图片并缩放至模版尺寸 /// </summary> /// <remarks>吴剑 2012-08-08</remarks> /// <param name="fromfile">原图Stream对象</param> /// <param name="fileSaveUrl">保存路径</param> /// <param name="maxWIDth">最大宽(单位:px)</param> /// <param name="maxHeight">最大高(单位:px)</param> /// <param name="quality">质量(范围0-100)</param> public static voID CutForCustom(System.IO.Stream fromfile,int maxWIDth,int maxHeight,int quality) { //从文件获取原始图片,并使用流中嵌入的颜色管理信息 System.Drawing.Image initimage = System.Drawing.Image.FromStream(fromfile,true); //原图宽高均小于模版,不作处理,直接保存 if (initimage.WIDth <= maxWIDth && initimage.Height <= maxHeight) { initimage.Save(fileSaveUrl,System.Drawing.Imaging.ImageFormat.Jpeg); } else { //模版的宽高比例 double templaterate = (double)maxWIDth / maxHeight; //原图片的宽高比例 double initRate = (double)initimage.WIDth / initimage.Height; //原图与模版比例相等,直接缩放 if (templaterate == initRate) { //按模版大小生成最终图片 System.Drawing.Image templateImage = new System.Drawing.Bitmap(maxWIDth,maxHeight); system.drawing.graphics templateG = system.drawing.graphics.FromImage(templateImage); templateG.InterpolationMode = System.Drawing.drawing2d.InterpolationMode.High; templateG.SmoothingMode = System.Drawing.drawing2d.SmoothingMode.HighQuality; templateG.Clear(color.White); templateG.DrawImage(initimage,maxWIDth,maxHeight),initimage.WIDth,initimage.Height),system.drawing.graphicsUnit.Pixel); templateImage.Save(fileSaveUrl,System.Drawing.Imaging.ImageFormat.Jpeg); } //原图与模版比例不等,裁剪后缩放 else { //裁剪对象 System.Drawing.Image pickedImage = null; system.drawing.graphics pickedG = null; //定位 Rectangle fromr = new Rectangle(0,0);//原图裁剪定位 Rectangle toR = new Rectangle(0,0);//目标定位 //宽为标准进行裁剪 if (templaterate > initRate) { //裁剪对象实例化 pickedImage = new System.Drawing.Bitmap(initimage.WIDth,(int)System.Math.Floor(initimage.WIDth / templaterate)); pickedG = system.drawing.graphics.FromImage(pickedImage); //裁剪源定位 fromr.X = 0; fromr.Y = (int)System.Math.Floor((initimage.Height - initimage.WIDth / templaterate) / 2); fromr.WIDth = initimage.WIDth; fromr.Height = (int)System.Math.Floor(initimage.WIDth / templaterate); //裁剪目标定位 toR.X = 0; toR.Y = 0; toR.WIDth = initimage.WIDth; toR.Height = (int)System.Math.Floor(initimage.WIDth / templaterate); } //高为标准进行裁剪 else { pickedImage = new System.Drawing.Bitmap((int)System.Math.Floor(initimage.Height * templaterate),initimage.Height); pickedG = system.drawing.graphics.FromImage(pickedImage); fromr.X = (int)System.Math.Floor((initimage.WIDth - initimage.Height * templaterate) / 2); fromr.Y = 0; fromr.WIDth = (int)System.Math.Floor(initimage.Height * templaterate); fromr.Height = initimage.Height; toR.X = 0; toR.Y = 0; toR.WIDth = (int)System.Math.Floor(initimage.Height * templaterate); toR.Height = initimage.Height; } //设置质量 pickedG.InterpolationMode = System.Drawing.drawing2d.InterpolationMode.HighQualityBicubic; pickedG.SmoothingMode = System.Drawing.drawing2d.SmoothingMode.HighQuality; //裁剪 pickedG.DrawImage(initimage,system.drawing.graphicsUnit.Pixel); //按模版大小生成最终图片 System.Drawing.Image templateImage = new System.Drawing.Bitmap(maxWIDth,maxHeight); system.drawing.graphics templateG = system.drawing.graphics.FromImage(templateImage); templateG.InterpolationMode = System.Drawing.drawing2d.InterpolationMode.High; templateG.SmoothingMode = System.Drawing.drawing2d.SmoothingMode.HighQuality; templateG.Clear(color.White); templateG.DrawImage(pickedImage,pickedImage.WIDth,pickedImage.Height),system.drawing.graphicsUnit.Pixel); //关键质量控制 //获取系统编码类型数组,tiff ImageCodecInfo[] icis = ImageCodecInfo.GetimageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo i in icis) { if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif") { ici = i; } } EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,(long)quality); //保存缩略图 templateImage.Save(fileSaveUrl,ep); //templateImage.Save(fileSaveUrl,System.Drawing.Imaging.ImageFormat.Jpeg); //释放资源 templateG.dispose(); templateImage.dispose(); pickedG.dispose(); pickedImage.dispose(); } } //释放资源 initimage.dispose(); } #endregion #region 等比缩放 /// <summary> /// 图片等比缩放 /// </summary> /// <remarks>吴剑 2012-08-08</remarks> /// <param name="fromfile">原图Stream对象</param> /// <param name="savePath">缩略图存放地址</param> /// <param name="targetWIDth">指定的最大宽度</param> /// <param name="targetHeight">指定的最大高度</param> /// <param name="watermarkText">水印文字(为""表示不使用水印)</param> /// <param name="watermarkImage">水印图片路径(为""表示不使用水印)</param> public static voID Zoomauto(System.IO.Stream fromfile,string savePath,System.Double targetWIDth,System.Double targetHeight,string watermarkText,string watermarkImage) { //创建目录 string dir = Path.GetDirectoryname(savePath); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息) System.Drawing.Image initimage = System.Drawing.Image.FromStream(fromfile,true); //原图宽高均小于模版,不作处理,直接保存 if (initimage.WIDth <= targetWIDth && initimage.Height <= targetHeight) { //文字水印 if (watermarkText != "") { using (system.drawing.graphics gWater = system.drawing.graphics.FromImage(initimage)) { System.Drawing.Font FontWater = new Font("黑体",10); System.Drawing.Brush brushWater = new SolIDBrush(color.White); gWater.DrawString(watermarkText,FontWater,brushWater,10,10); gWater.dispose(); } } //透明图片水印 if (watermarkImage != "") { if (file.Exists(watermarkImage)) { //获取水印图片 using (System.Drawing.Image wrImage = System.Drawing.Image.Fromfile(watermarkImage)) { //水印绘制条件:原始图片宽高均大于或等于水印图片 if (initimage.WIDth >= wrImage.WIDth && initimage.Height >= wrImage.Height) { Graphics gWater = Graphics.FromImage(initimage); //透明属性 ImageAttributes imgAttributes = new ImageAttributes(); colorMap colorMap = new colorMap(); colorMap.oldcolor = color.FromArgb(255,255,0); colorMap.Newcolor = color.FromArgb(0,0); colorMap[] remaptable = { colorMap }; imgAttributes.SetRemaptable(remaptable,colorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] {1.0f,0.0f,0.0f},new float[] {0.0f,1.0f,0.5f,//透明度:0.5 new float[] {0.0f,1.0f} }; colorMatrix wmcolorMatrix = new colorMatrix(colorMatrixElements); imgAttributes.SetcolorMatrix(wmcolorMatrix,colorMatrixFlag.Default,colorAdjustType.Bitmap); gWater.DrawImage(wrImage,new Rectangle(initimage.WIDth - wrImage.WIDth,initimage.Height - wrImage.Height,wrImage.WIDth,wrImage.Height),wrImage.Height,GraphicsUnit.Pixel,imgAttributes); gWater.dispose(); } wrImage.dispose(); } } } //保存 initimage.Save(savePath,System.Drawing.Imaging.ImageFormat.Jpeg); } else { //缩略图宽、高计算 double newWIDth = initimage.WIDth; double newHeight = initimage.Height; //宽大于高或宽等于高(横图或正方) if (initimage.WIDth > initimage.Height || initimage.WIDth == initimage.Height) { //如果宽大于模版 if (initimage.WIDth > targetWIDth) { //宽按模版,高按比例缩放 newWIDth = targetWIDth; newHeight = initimage.Height * (targetWIDth / initimage.WIDth); } } //高大于宽(竖图) else { //如果高大于模版 if (initimage.Height > targetHeight) { //高按模版,宽按比例缩放 newHeight = targetHeight; newWIDth = initimage.WIDth * (targetHeight / initimage.Height); } } //生成新图 //新建一个bmp图片 System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWIDth,(int)newHeight); //新建一个画板 system.drawing.graphics newG = system.drawing.graphics.FromImage(newImage); //设置质量 newG.InterpolationMode = System.Drawing.drawing2d.InterpolationMode.HighQualityBicubic; newG.SmoothingMode = System.Drawing.drawing2d.SmoothingMode.HighQuality; //置背景色 newG.Clear(color.White); //画图 newG.DrawImage(initimage,newImage.WIDth,newImage.Height),system.drawing.graphicsUnit.Pixel); //文字水印 if (watermarkText != "") { using (system.drawing.graphics gWater = system.drawing.graphics.FromImage(newImage)) { System.Drawing.Font FontWater = new Font("宋体",10); gWater.dispose(); } } //透明图片水印 if (watermarkImage != "") { if (file.Exists(watermarkImage)) { //获取水印图片 using (System.Drawing.Image wrImage = System.Drawing.Image.Fromfile(watermarkImage)) { //水印绘制条件:原始图片宽高均大于或等于水印图片 if (newImage.WIDth >= wrImage.WIDth && newImage.Height >= wrImage.Height) { Graphics gWater = Graphics.FromImage(newImage); //透明属性 ImageAttributes imgAttributes = new ImageAttributes(); colorMap colorMap = new colorMap(); colorMap.oldcolor = color.FromArgb(255,new Rectangle(newImage.WIDth - wrImage.WIDth,newImage.Height - wrImage.Height,imgAttributes); gWater.dispose(); } wrImage.dispose(); } } } //保存缩略图 newImage.Save(savePath,System.Drawing.Imaging.ImageFormat.Jpeg); //释放资源 newG.dispose(); newImage.dispose(); initimage.dispose(); } } #endregion #region 其它 /// <summary> /// 判断文件类型是否为WEB格式图片 /// (注:JPG,GIF,BMP,PNG) /// </summary> /// <param name="ContentType">httpPostedfile.ContentType</param> /// <returns></returns> public static bool IsWebImage(string ContentType) { if (ContentType == "image/pjpeg" || ContentType == "image/jpeg" || ContentType == "image/gif" || ContentType == "image/bmp" || ContentType == "image/png") { return true; } else { return false; } } #endregion }//end class }
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的C#图片处理示例(裁剪,缩放,清晰度,水印)全部内容,希望文章能够帮你解决C#图片处理示例(裁剪,缩放,清晰度,水印)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)