C#.NET 添加图片水印
2013-11-25 17:09
393 查看
using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.IO;namespace Pub.Class{ ///<summary>/// 添加水印类 只支持添加图片水印 ///</summary>publicclass Watermark { #region 私有成员 privatestring modifyImagePath=null; privatestring drawedImagePath=null; privateint rightSpace; privateint bottoamSpace; privateint lucencyPercent=70; privatestring outPath=null; #endregion#region 构造器 ///<summary>/// 构造函数 ///</summary>public Watermark() { } #endregion#region 属性 ///<summary>/// 获取或设置要修改的图像路径 ///</summary>publicstring ModifyImagePath { get{returnthis.modifyImagePath;} set{this.modifyImagePath=value;} } ///<summary>/// 获取或设置在画的图片路径(水印图片) ///</summary>publicstring DrawedImagePath { get{returnthis.drawedImagePath;} set{this.drawedImagePath=value;} } ///<summary>/// 获取或设置水印在修改图片中的右边距 ///</summary>publicint RightSpace { get{returnthis.rightSpace;} set{this.rightSpace=value;} } ///<summary>/// 获取或设置水印在修改图片中距底部的高度 ///</summary>publicint BottoamSpace { get{returnthis.bottoamSpace;} set{this.bottoamSpace=value;} } ///<summary>/// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比 ///</summary>publicint LucencyPercent { get{returnthis.lucencyPercent;} set { if(value>=0&&value<=100) this.lucencyPercent=value; } } ///<summary>/// 获取或设置要输出图像的路径 ///</summary>publicstring OutPath { get{returnthis.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>publicvoid 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 ={//设置颜色矩阵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}}; 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 }}
相关文章推荐
- C#.NET 添加图片水印
- C#.NET 添加图片水印
- C#.NET 图片水印添加代码
- asp.net、C#动态添加图片水印
- .net c# gif动画如何添加图片水印实现思路及代码
- .net c# gif动画如何添加图片水印实现思路及代码
- ASP.NET(C#)缩略图片,添加文字水印和图片水印
- asp .net 为图片添加图片水印 .
- C# ASP.NET MVC 图片盗链 加水印 的问题
- C#/VB.NET对EXCEL图片添加超链接
- Asp.net(C#)给图片加上水印效果
- ASP.NET为图片添加水印功能示例详解
- C#给图片添加水印完整实例
- c# 添加图片水印,可以指定水印位置+生成缩略图
- asp .net 为图片添加文字水印(内包含有加图片水印的方法,但尚未完善暂不能使用)
- asp.net中上传图片并生成小图片,自动添加水印的代码
- asp.net生成缩略图,添加图片水印的类
- c# 添加图片水印,可以指定水印位置+生成缩略图
- Asp.net(C#)给图片加上水印效果
- C# 添加水印图片、文字、缩略图处理