先看效果图:
不多说,具体操作看代码,注释我写的很详细.......水印的Demo还是放在资源里面......
1.ImgWaterMark.aspx页面展示-水印图片
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ImgWaterMark.aspx.cs" Inherits="WaterMarkPrj._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>图片水印</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fuImage" runat="server" />
<asp:ImageButton ID="imgBtnWaterMark" ImageUrl="~/Images/sztp.jpg" runat="server"
OnClick="imgBtnWaterMark_Click" />
</div>
</form>
</body>
</html>
2.ImgWaterMark.aspx.cs后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WaterMarkPrj
{
public partial class _Default : System.Web.UI.Page
{
#region Attribute
PictureSet ps = new PictureSet();
#endregion
#region Page_Load
/// <summary>
/// 页面加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
}
}
#endregion
#region Function
#endregion
#region Event
/// <summary>
/// 设置水印图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void imgBtnWaterMark_Click(object sender, ImageClickEventArgs e)
{
//获取图片路径-这里设置的是以上传的图片,可以自行改动,只需要获取其路径即可
string filePath = Server.MapPath("~/UpLoadFile/") + fuImage.FileName;
ps.ImgWaterMark(filePath);
ClientScript.RegisterStartupScript(GetType(), "", "alert('添加图片水印成功!');", true);
}
#endregion
}
}
3.LetterWaterMark.aspx页面展示-水印文字
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LetterWaterMark.aspx.cs"
Inherits="WaterMarkPrj.LetterWaterMark" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>文字水印</title>
</head>
<body>
<form id="form1" runat="server">
<div>
水印文字:<asp:TextBox ID="txtLetter" runat="server"></asp:TextBox>
<asp:FileUpload ID="fuImage" runat="server" />
<asp:ImageButton ID="imgBtnWaterMark" ImageUrl="~/Images/szwz.jpg" runat="server"
OnClick="imgBtnWaterMark_Click" />
</div>
</form>
</body>
</html>
4.LetterWaterMark.aspx.cs后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WaterMarkPrj
{
public partial class LetterWaterMark : System.Web.UI.Page
{
#region Attribute
PictureSet ps = new PictureSet();
#endregion
#region Page_Load
/// <summary>
/// 页面加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
}
}
#endregion
#region Function
#endregion
#region Event
/// <summary>
/// 设置水印文字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void imgBtnWaterMark_Click(object sender, ImageClickEventArgs e)
{
//获取要设置的水印文字
string str = this.txtLetter.Text.Trim().ToString();
//获取图片路径-这里设置的是以上传的图片,可以自行改动,只需要获取其路径即可
string filePath = Server.MapPath("~/UpLoadFile/") + fuImage.FileName;
ps.LetterWaterMark(filePath, str);
ClientScript.RegisterStartupScript(GetType(), "", "alert('添加水印文字成功!');", true);
}
#endregion
}
}
5.PictureSet.cs公共方法类-调用添加水印的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace WaterMarkPrj
{
public class PictureSet
{
#region 设置图片水印
/// <summary>
/// 设置水印图片
/// </summary>
/// <param name="path">要设置水印图片的路径</param>
public void ImgWaterMark(string path)
{
//获取水印图片-Bitmap类是封装GDI+位图
Bitmap bmp = new Bitmap(HttpContext.Current.Server.MapPath("~") + "/Images/Alex.GIF");
//设置水印图片的透明度
ImageAttributes imgAttr = new ImageAttributes();
//.SetColorKey(Color colorLow,Color colorHigh)-默认类别设置颜色键;GetPixel()-获取Bitmap中指定像素的颜色
imgAttr.SetColorKey(bmp.GetPixel(20, 20), bmp.GetPixel(20, 20));
//获取要设置水印图片的扩展名
string extension = Path.GetExtension(path).ToUpper();
//设置临时图片的名称
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
//初始化要加水印的图片
Image image = Image.FromFile(path);
//初始化水印图片
Image copyImage = Image.FromFile(HttpContext.Current.Server.MapPath("~") + "/Images/Alex.GIF");
//创建绘图区域
Graphics g = Graphics.FromImage(image);
//将水印图片合成到指定图片上
g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, imgAttr);
//释放
g.Dispose();
//保存加水印后的图片,删除原始图片
string newPath = HttpContext.Current.Server.MapPath(".") + "/" + fileName + "_new" + extension;
//保存设置完成后的水印图片到临时位置
image.Save(newPath);
//释放
image.Dispose();
File.Copy(newPath, path, true);
//删除水印
if (File.Exists(newPath))
{
//删除临时存储的图片
File.Delete(newPath);
}
}
#endregion
#region 设置文字水印
/// <summary>
/// 设置水印文字
/// </summary>
/// <param name="path">要设置水印图片的路径</param>
/// <param name="str">水印文字</param>
public void LetterWaterMark(string path, string str)
{
//获取文件扩展名
string extension = Path.GetExtension(path);
//设置临时文件名称
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
//加文字水印
//初始化要加水印的图片
Image image = Image.FromFile(path);
//创建绘图区域
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
//字体
Font f = new Font("楷体", 50);
//填充图形形状
Brush b = new SolidBrush(Color.Red);
g.DrawString(str, f, b, 10, 10);
g.Dispose();
//保存加文字水印后的图片,删除原始图片
string newPath = HttpContext.Current.Server.MapPath(".") + "/" + fileName + "_new" + extension;
image.Save(newPath);
image.Dispose();
//将水印图片替换原来图片
File.Copy(newPath, path, true);
//删除水印
if (File.Exists(newPath))
{
File.Delete(newPath);
}
}
#endregion
}
}