dw个人网站主页怎么做,计算机培训机构排名前十,学做馒头面包哪个网站好,游戏网站建设方案圣诞节#xff0c;25日#xff0c;要交ACCP5.0认证的项目#xff0c;其中有这样一个要求#xff1a;书店的所有图书的封面放在了\images\convers\下面#xff0c;要求所有引用这一路径下的图片都添加书店的店名水印图片。就是说拦截Http请求了#xff0c;自然想到HttpHan…圣诞节25日要交ACCP5.0认证的项目其中有这样一个要求书店的所有图书的封面放在了\images\convers\下面要求所有引用这一路径下的图片都添加书店的店名水印图片。就是说拦截Http请求了自然想到HttpHandler可以办到。考虑下实现的效果应该是这样的为了通用监视的路径水印图片路径默认图片路径3者应该在配置文件里面设定方便修改监视路径下的所有图片只要物理存在都要有水印物理不存在用默认图片替代若水印图片不存在用文字代替。访问其他路径下的图片应该正常显示没有水印....废话不多说实现的代码如下为方便调试编译httpHandler类的时候要加调试选项并在项目中引用这个dll编译csc /t:library WatermarkHandler.cs /debug)using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;namespace xumh{ /**//// summary /// 图片处理模块给指定路径下的图片添加水印输出 /// 1.监视的目录路径水印图片路径默认图片三者都在web.config文件中设定 /// 2.请求的图片的路径不属于监视路径直接输出图片 /// 3.请求的图片的路径属于监视路径若图片不存在显示默认图片不加水印 /// 4.请求的图片的路径属于监视路径且文件存在添加水印 /// 水印图片存在图片水印图片输出 /// 水印图片不存在图片水印文字输出 /// /// 5.web.config部分如下 /// appSettings /// !--要监视的路径-- /// add keymonitorPath value/images/bookcovers// /// !--图书封面不存在时显示的默认封面图片的相对路径-- /// add keydefaultImage valueimages\default.jpg/ /// !--相对于物理应用程序的根路径的水印图片的相对路径-- /// add keywatermarkImage valueimages\WaterMark.jpg/ /// /appSettings /// /// httpHandlers /// add verb* path*.jpg typexumh.WatermarkHandler,WatermarkHandler/ /// /httpHandlers /// 许明会 2007-12-23 18:00 Visual Studio 2005 调试通过 /// /summary public class WatermarkHandler:IHttpHandler { public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { string url context.Request.Url.AbsoluteUri.ToLower(); string monitorPath System.Configuration.ConfigurationManager.AppSettings[monitorPath]; //若图片不属于封面直接输出图片:若图片不存在不做处理 bool IsInterestUrl url.Contains(monitorPath); System.Drawing.Image imgSource null; if (!IsInterestUrl) { if (!System.IO.File.Exists(context.Request.PhysicalPath)) return; imgSource System.Drawing.Image.FromFile(context.Request.PhysicalPath); imgSource.Save(context.Response.OutputStream, imgSource.RawFormat); imgSource.Dispose(); return; } //图片属于封面但图片imageSource不存在显示默认图片default.jpg string physicalPath context.Request.PhysicalPath;// context.Server.MapPath(url); if (!System.IO.File.Exists(physicalPath)) { string defaultImage System.Configuration.ConfigurationManager.AppSettings[defaultImage]; imgSource System.Drawing.Image.FromFile(context.Request.PhysicalApplicationPath defaultImage); imgSource.Save(context.Response.OutputStream, imgSource.RawFormat); imgSource.Dispose(); return; } //----------------------只要有封面图片就为图片添加水印------------------------- string watermarkImage System.Configuration.ConfigurationManager.AppSettings[watermarkImage]; string watermarkImagePath context.Request.PhysicalApplicationPath watermarkImage; bool bWatermarkImageExist System.IO.File.Exists(watermarkImagePath); imgSource System.Drawing.Image.FromFile(physicalPath); System.Drawing.Graphics graphic System.Drawing.Graphics.FromImage(imgSource); if (bWatermarkImageExist) //“水印图片”存在:存在则封面混合水印图片 { System.Drawing.Image imgWatermark System.Drawing.Image.FromFile(watermarkImagePath); graphic.DrawImage(imgWatermark, imgSource.Width - imgWatermark.Width, imgSource.Height - imgWatermark.Height, imgWatermark.Width, imgWatermark.Height); //把图片保存到输出流 imgSource.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); imgWatermark.Dispose(); } else //“水印图片”不存在:输出文本 { System.Drawing.Font font new System.Drawing.Font(幼圆, 13.0f,System.Drawing.FontStyle.Bold); graphic.DrawString(第三波书店,font , System.Drawing.Brushes.Red, new System.Drawing.PointF()); imgSource.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } imgSource.Dispose(); graphic.Dispose(); //标明类型为jpg如果不标注IE没有问题但Firefox会出现乱码 context.Response.ContentType image/jpeg; context.Response.Flush(); context.Response.End(); } }} 转载于:https://www.cnblogs.com/flaaash/archive/2007/12/23/1011640.html