C#.NET 图片水印添加代码
程序员文章站
2023-02-02 15:05:03
本文实例为大家分享了c#.net添加 图片水印的方法,供大家参考,具体内容如下
using system;
using system.drawing;
usi...
本文实例为大家分享了c#.net添加 图片水印的方法,供大家参考,具体内容如下
using system; using system.drawing; using system.drawing.drawing2d; using system.drawing.imaging; using system.io; namespace pub.class { /// <summary> /// 添加水印类 只支持添加图片水印 /// </summary> public class watermark { #region 私有成员 private string modifyimagepath=null; private string drawedimagepath=null; private int rightspace; private int bottoamspace; private int lucencypercent=70; private string outpath=null; #endregion #region 构造器 /// <summary> /// 构造函数 /// </summary> public watermark() { } #endregion #region 属性 /// <summary> /// 获取或设置要修改的图像路径 /// </summary> public string modifyimagepath { get{return this.modifyimagepath;} set{this.modifyimagepath=value;} } /// <summary> /// 获取或设置在画的图片路径(水印图片) /// </summary> public string drawedimagepath { get{return this.drawedimagepath;} set{this.drawedimagepath=value;} } /// <summary> /// 获取或设置水印在修改图片中的右边距 /// </summary> public int rightspace { get{return this.rightspace;} set{this.rightspace=value;} } /// <summary> /// 获取或设置水印在修改图片中距底部的高度 /// </summary> public int bottoamspace { get{return this.bottoamspace;} set{this.bottoamspace=value;} } /// <summary> /// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比 /// </summary> public int lucencypercent { get{return this.lucencypercent;} set { if(value>=0&&value<=100) this.lucencypercent=value; } } /// <summary> /// 获取或设置要输出图像的路径 /// </summary> public string outpath { get{return this.outpath;} set{this.outpath=value;} } #endregion #region 开始绘制水印 drawimage /// <summary> /// 开始绘制水印 /// </summary> /// <example> /// <code> /// watermark wm = new watermark(); /// wm.drawedimagepath= server.mappath("") + "/upfile/" + "backlogo.gif"; /// wm.modifyimagepath=path; /// wm.rightspace=184; /// wm.bottoamspace=81; /// wm.lucencypercent=50; /// wm.outpath=server.mappath("") + "/upfile/" + filename + "_new" + extension; /// wm.drawimage(); /// /// //保存加水印过后的图片,删除原始图片 /// mfilename=filename + "_new" + extension; /// if(file.exists(path)) { file.delete(path); } /// </code> /// </example> public void drawimage() { image modifyimage=null; image drawedimage=null; graphics g=null; try { modifyimage=image.fromfile(this.modifyimagepath);//建立图形对象 drawedimage=image.fromfile(this.drawedimagepath); g=graphics.fromimage(modifyimage); int x=modifyimage.width-this.rightspace;//获取要绘制图形坐标 int y=modifyimage.height-this.bottoamspace; float[][] matrixitems ={//设置颜色矩阵 new float[] {1, 0, 0, 0, 0}, new float[] {0, 1, 0, 0, 0}, new float[] {0, 0, 1, 0, 0}, new float[] {0, 0, 0, (float)this.lucencypercent/100f, 0}, new float[] {0, 0, 0, 0, 1}}; colormatrix colormatrix = new colormatrix(matrixitems); imageattributes imgattr=new imageattributes(); imgattr.setcolormatrix(colormatrix,colormatrixflag.default,coloradjusttype.bitmap); g.drawimage(//绘制阴影图像 drawedimage, new rectangle(x,y,drawedimage.width,drawedimage.height), 0,0,drawedimage.width,drawedimage.height, graphicsunit.pixel,imgattr); string[] allowimagetype={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};//保存文件 fileinfo file=new fileinfo(this.modifyimagepath); imageformat imagetype=imageformat.gif; switch(file.extension.tolower()) { case ".jpg": imagetype=imageformat.jpeg; break; case ".gif": imagetype=imageformat.gif; break; case ".png": imagetype=imageformat.png; break; case ".bmp": imagetype=imageformat.bmp; break; case ".tif": imagetype=imageformat.tiff; break; case ".wmf": imagetype=imageformat.wmf; break; case ".ico": imagetype=imageformat.icon; break; default: break; } memorystream ms=new memorystream(); modifyimage.save(ms,imagetype); byte[] imgdata=ms.toarray(); modifyimage.dispose(); drawedimage.dispose(); g.dispose(); filestream fs=null; if(this.outpath==null || this.outpath=="") { file.delete(this.modifyimagepath); fs=new filestream(this.modifyimagepath,filemode.create,fileaccess.write); } else { fs=new filestream(this.outpath,filemode.create,fileaccess.write); } if(fs!=null) { fs.write(imgdata,0,imgdata.length); fs.close(); } } finally { try { drawedimage.dispose(); modifyimage.dispose(); g.dispose(); } catch{} } } #endregion } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。