您好,欢迎访问一九零五行业门户网

C#图片处理示例(裁剪,缩放,清晰度,水印)

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 { public class image { #region 正方型裁剪并缩放 /// /// 正方型裁剪 /// 以图片中心为轴心,截取正方型,然后等比缩放 /// 用于头像处理 /// /// 吴剑 2012-08-08 /// 原图stream对象 /// 缩略图存放地址 /// 指定的边长(正方型) /// 质量(范围0-100) 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 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, 0, initheight, initheight); rectangle tor = new rectangle(0, 0, initheight, 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, 0, initwidth, initwidth); //画图 pickedg.drawimage(initimage, tor, fromr, 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, 0, side, side), new system.drawing.rectangle(0, 0, initwidth, 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 自定义裁剪并缩放 /// /// 指定长宽裁剪 /// 按模版比例最大范围的裁剪图片并缩放至模版尺寸 /// /// 吴剑 2012-08-08 /// 原图stream对象 /// 保存路径 /// 最大宽(单位:px) /// 最大高(单位:px) /// 质量(范围0-100) public static void cutforcustom(system.io.stream fromfile, string filesaveurl, int maxwidth, int maxheight, int quality) { //从文件获取原始图片,并使用流中嵌入的颜色管理信息 system.drawing.image initimage = system.drawing.image.fromstream(fromfile, true); //原图宽高均小于模版,不作处理,直接保存 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, new system.drawing.rectangle(0, 0, newimage.width, newimage.height), new system.drawing.rectangle(0, 0, initimage.width, initimage.height), system.drawing.graphicsunit.pixel); //文字水印 if (watermarktext != ) { using (system.drawing.graphics gwater = system.drawing.graphics.fromimage(newimage)) { 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 (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, 0, 255, 0); colormap.newcolor = color.fromargb(0, 0, 0, 0); colormap[] remaptable = { colormap }; imgattributes.setremaptable(remaptable, coloradjusttype.bitmap); float[][] colormatrixelements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.5f, 0.0f},//透明度:0.5 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; colormatrix wmcolormatrix = new colormatrix(colormatrixelements); imgattributes.setcolormatrix(wmcolormatrix, colormatrixflag.default, coloradjusttype.bitmap); gwater.drawimage(wrimage, new rectangle(newimage.width - wrimage.width, newimage.height - wrimage.height, wrimage.width, wrimage.height), 0, 0, wrimage.width, wrimage.height, graphicsunit.pixel, imgattributes); gwater.dispose(); } wrimage.dispose(); } } } //保存缩略图 newimage.save(savepath, system.drawing.imaging.imageformat.jpeg); //释放资源 newg.dispose(); newimage.dispose(); initimage.dispose(); } } #endregion #region 其它 /// /// 判断文件类型是否为web格式图片 /// (注:jpg,gif,bmp,png) /// /// httppostedfile.contenttype /// 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 }
其它类似信息

推荐信息