先看效果
下面有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>
您还没有登录,请您登录后再发表评论
http属于TCP/IP 模型中的应用层协议,而两个应用程序之间要进行通信,首先得建立TCP连接,然后浏览器才能向服务器发送请求信息,服务器在接收到请求信息后,返回相应的应答信息,浏览器接收到来自服务器的应答信息后...
HttpModule,HttpHandler详细解释
第一,HttpHandle是实现不了urlrewrite的;第二Server.Transfer是标准的重定向,根本不是urlrewrite。 其实,实现urlrewrite不用自己HttpHandle,也不用自己实现HttpModule,用几行代码就可以轻松实现。 我这里介绍的...
这个程序使用了ASP.Net下的Web.config配置简单的实现了图片的防盗功能
asp.net HttpHandler操作Session的函数代码,需要的朋友可以参考下。
相关推荐
http属于TCP/IP 模型中的应用层协议,而两个应用程序之间要进行通信,首先得建立TCP连接,然后浏览器才能向服务器发送请求信息,服务器在接收到请求信息后,返回相应的应答信息,浏览器接收到来自服务器的应答信息后...
HttpModule,HttpHandler详细解释
第一,HttpHandle是实现不了urlrewrite的;第二Server.Transfer是标准的重定向,根本不是urlrewrite。 其实,实现urlrewrite不用自己HttpHandle,也不用自己实现HttpModule,用几行代码就可以轻松实现。 我这里介绍的...
这个程序使用了ASP.Net下的Web.config配置简单的实现了图片的防盗功能
asp.net HttpHandler操作Session的函数代码,需要的朋友可以参考下。