使用Httphandle 实现水印效果

先看效果

下面有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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值