C#.NET 添加图片水印
usingSystem;usingSystem.Drawing;usingSystem.Drawing.Drawing2D;usingSystem.Drawing.Imaging;usingSystem.IO;namespacePub.Class{///<summary>///添加水印类只支持添加图片水印///</summary>publicclassWatermark{#region 私有成员 privatestring modifyImagePath=null;privatestring drawedImagePath=null;privateintrightSpace;privateintbottoamSpace;privateint lucencyPercent=70;privatestring outPath=null;#endregion#region 构造器 ///<summary>///构造函数///</summary>publicWatermark(){}#endregion#region 属性 ///<summary>///获取或设置要修改的图像路径///</summary>publicstringModifyImagePath{get{returnthis.modifyImagePath;}set{this.modifyImagePath=value;}}///<summary>///获取或设置在画的图片路径(水印图片)///</summary>publicstringDrawedImagePath{get{returnthis.drawedImagePath;}set{this.drawedImagePath=value;}}///<summary>///获取或设置水印在修改图片中的右边距///</summary>publicintRightSpace{get{returnthis.rightSpace;}set{this.rightSpace=value;}}///<summary>///获取或设置水印在修改图片中距底部的高度///</summary>publicintBottoamSpace{get{returnthis.bottoamSpace;}set{this.bottoamSpace=value;}}///<summary>///获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比///</summary>publicintLucencyPercent{get{returnthis.lucencyPercent;}set { if(value>=0&&value<=100) this.lucencyPercent=value;}}///<summary>///获取或设置要输出图像的路径///</summary>publicstringOutPath{get{returnthis.outPath;}set{this.outPath=value;}}#endregion#region 开始绘制水印 DrawImage ///<summary>///开始绘制水印///</summary>///<example>///<code>///Watermarkwm=newWatermark();///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>publicvoidDrawImage(){ImagemodifyImage=null;ImagedrawedImage=null;Graphicsg=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 ={//设置颜色矩阵newfloat[] {1, 0, 0, 0, 0},newfloat[] {0, 1, 0, 0, 0},newfloat[] {0, 0, 1, 0, 0},newfloat[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},newfloat[] {0, 0, 0, 0, 1}};ColorMatrixcolorMatrix= newColorMatrix(matrixItems);ImageAttributesimgAttr=newImageAttributes();imgAttr.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);g.DrawImage(//绘制阴影图像drawedImage,newRectangle(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);ImageFormatp_w_picpathType=ImageFormat.Gif;switch(file.Extension.ToLower()){case".jpg": p_w_picpathType=ImageFormat.Jpeg; break;case".gif": p_w_picpathType=ImageFormat.Gif; break;case".png": p_w_picpathType=ImageFormat.Png; break;case".bmp": p_w_picpathType=ImageFormat.Bmp; break;case".tif": p_w_picpathType=ImageFormat.Tiff; break;case".wmf": p_w_picpathType=ImageFormat.Wmf; break;case".ico": p_w_picpathType=ImageFormat.Icon; break;default: break;}MemoryStreamms=newMemoryStream();modifyImage.Save(ms,p_w_picpathType);byte[] imgData=ms.ToArray();modifyImage.Dispose();drawedImage.Dispose();g.Dispose();FileStreamfs=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}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。