先看效果
下面有2张图片一个有水印的,一个无水印的.
代码:
首先我们定义处理图片的类
public class ImageHandler:IHttpHandler
{
public ImageHandler()
{
//
// TODO: Add constructor logic here
//
}
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
string imagePath = context.Request.PhysicalPath;
Bitmap image = null;
//如果挡墙缓存中不存在当前图片则为该图片添加水印并缓存
if (context.Cache[imagePath] == null)
{
image = new Bitmap(imagePath);
image = AddColorImage(image);
context.Cache[imagePath] = image;
}
else
{
//从缓存中获取当前图片
image = context.Cache[imagePath] as Bitmap;
}
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
//添加水印
public static Bitmap AddColorImage(Bitmap image)
{
string text = ConfigurationManager.AppSettings["waterMark"].ToString();
int fontSize = int.Parse(ConfigurationManager.AppSettings["fontSize"].ToString());
Font f = new Font("宋体", fontSize);
//定义刷子的颜色
Brush b = Brushes.Purple;
//从指定的Image 获取一个新的 Graphics
Graphics g = Graphics.FromImage(image);
//用指定的Font 绘制指定的字符串返回一个SizeF结构,该结构表示由指定的text,使用 font 绘制的字符串大小
SizeF sf = g.MeasureString(text, f);
g.DrawString(text, f, b, image.Width - sf.Width, image.Height - sf.Height);
g.Dispose();
return image;
}
}
然后定义一个html 页面
<table class="style1">
<tr>
<td colspan="2" style="text-align: center">
<h1>
水印图片</h1>
</td>
</tr>
<tr>
<td style="text-align: right">
图片(无水印):</td>
<td>
<img src="hangzhou.PNG" /></td>
</tr>
<tr>
<td style="text-align: right">
图片:</td>
<td>
<img src="UploadImage/GuoHu.PNG" /></td>
</tr>
</table>
Webconfig:
<configuration>
<appSettings>
<!--添加到图片上的水印文字-->
<add key="waterMark" value="http://blog.csdn.net/ws_hgo"/>
<!--字体大小-->
<add key="fontSize" value="24"/>
</appSettings>
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<httpHandlers>
<!--只对文件夹UploadImage下面请求的文件映射到类ImageHandler-->
<add path="UploadImage/*.*" verb="*" type="ImageHandler" />
</httpHandlers>
<compilation debug="true" targetFramework="4.0"/>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/"/>
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/"/>
</providers>
</roleManager>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>