Csharp/C#教程:C#.NET 图片水印添加代码分享

本文实例为大家分享了C#.NET添加图片水印的方法,供大家参考,具体内容如下

usingSystem; usingSystem.Drawing; usingSystem.Drawing.Drawing2D; usingSystem.Drawing.Imaging; usingSystem.IO; namespacePub.Class { ///<summary> ///添加水印类只支持添加图片水印 ///</summary> publicclassWatermark { #region私有成员 privatestringmodifyImagePath=null; privatestringdrawedImagePath=null; privateintrightSpace; privateintbottoamSpace; privateintlucencyPercent=70; privatestringoutPath=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); intx=modifyImage.Width-this.rightSpace;//获取要绘制图形坐标 inty=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"};//保存文件 FileInfofile=newFileInfo(this.ModifyImagePath); ImageFormatimageType=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; } MemoryStreamms=newMemoryStream(); modifyImage.Save(ms,imageType); byte[]imgData=ms.ToArray(); modifyImage.Dispose(); drawedImage.Dispose(); g.Dispose(); FileStreamfs=null; if(this.OutPath==null||this.OutPath==""){ File.Delete(this.ModifyImagePath); fs=newFileStream(this.ModifyImagePath,FileMode.Create,FileAccess.Write); }else{ fs=newFileStream(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 } } 您可能感兴趣的文章:C#图片添加水印的实现代码C#实现给图片加水印的方法C#给图片添加水印完整实例C#监控文件夹并自动给图片文件打水印的方法C#给图片加水印的简单实现方法C#(.net)水印图片的生成完整实例c#图片添加水印的实例代码.netc#gif动画如何添加图片水印实现思路及代码C#添加图片水印类实现代码C#添加文字水印类代码

标签: 图片 .NET

基于Opencv实现颜色识别

opencv摄像头捕获识别颜色

上述就是C#学习教程:C#.NET 图片水印添加代码分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/907479.html

(0)
上一篇 2021年10月24日
下一篇 2021年10月24日

精彩推荐