回到目录

说在前

对于资源列表页来说,我们经常会把图像做成N多种,大图,小图,中图等等,很是麻烦,在数据迁移时,更是一种痛快,而如果你把图像资源部署到nginx上,那么这种图像缩放就变得很容易了,因为它有自己的过滤器来实现这个功能,只要程序员简单的配置即可(GraphicsMagick),其实在nginx上实现缩略图的方式有很多,而对于IIS服务来说,实现这种缩略图就没有直接的方法了,必须开发人员自己写代码来实现,下面解释两个比较早的技术(被执行的期间比较早,在页面渲染之前)HttpModule和httpHandler,这两个东西我在之前的文章中也已经讲过,细节不再重复。[HttpModule几大事件,HttpHandler实现图像防盗链]

做在后

一 HttpModule对URL地址进行重写,将扩展名为jpg,jpeg,png,gif结尾的URL地址进行复写,让它支持漂亮的缩略图参数,如原地址为:new1.jpg,实现缩略图原地址为:new1.jpg?w=100&h=100,进行Url重写后的漂亮地址为:new1_100x100.jpg,怎么样,是否是很漂亮,有点像MVC 的router吧,呵呵

    /// <summary>/// 实现URL的重写/// </summary>public class UrlRewriteModule : IHttpModule{#region IHttpModule 成员public void Init(HttpApplication context){context.BeginRequest += new EventHandler(Application_BeginRequest);}/// <summary>/// url重写/// .png?w=100&h=100/// _100x100.png/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void Application_BeginRequest(Object sender, EventArgs e){string oldUrl = HttpContext.Current.Request.RawUrl;if (oldUrl.LastIndexOf(".") > -1){string ext = oldUrl.Substring(oldUrl.LastIndexOf(".")).ToLower();//是图像文件if (ext == ".jpg"||ext == ".jpeg"||ext == ".png"||ext == "gif"){var param = oldUrl.Substring(oldUrl.LastIndexOf("_") + 1, (oldUrl.IndexOf(".") - oldUrl.LastIndexOf("_") - 1)).Split(new char[] { 'x' }, StringSplitOptions.RemoveEmptyEntries);//有图像缩放请求if (oldUrl.LastIndexOf("_") > -1){string newUrl = oldUrl.Substring(0, oldUrl.LastIndexOf("_"));newUrl = string.Format(newUrl + ext + "?w={0}&h={1}", param[0], param[1]);//将请求中的URL进行重写
                        HttpContext.Current.RewritePath(newUrl);}}}}#endregion#region IHttpModule 成员public void Dispose(){}#endregion}

二 使用HttpHandler进行对图像的缩放,你的服务器是指与图像资源在一起的那台电脑

    /// <summary>/// 图片动态缩放处理程序/// </summary>public class ImageScalingHandler : IHttpHandler{/// <summary>/// 图像等比例缩放,图像默认为白色/// </summary>/// <param name="image"></param>/// <param name="width"></param>/// <param name="height"></param>/// <returns></returns>private Bitmap CreateThumbnail(Image image, int width, int height){Point point = new Point(0, 0); //图像从那个坐标点进行截取double wRate = 1, hRate = 1, setRate = 1;int newWidth = 0, newHeight = 0;try{if (width == 0) width = image.Width;if (height == 0) height = image.Height;if (image.Height > height){hRate = (double)height / image.Height;}if (image.Width > width){wRate = (double)width / image.Width;}if (wRate != 1 || hRate != 1){if (wRate > hRate){setRate = hRate;}else{setRate = wRate;}}newWidth = (int)(image.Width * setRate);newHeight = (int)(image.Height * setRate);if (height > newHeight){point.Y = Convert.ToInt32(height / 2 - newHeight / 2);}if (width > newWidth){point.X = Convert.ToInt32(width / 2 - newWidth / 2);}Bitmap bit = new Bitmap(width, height);Rectangle r = new Rectangle(point.X, point.Y, (int)(image.Width * setRate), (int)(image.Height * setRate));Graphics g = Graphics.FromImage(bit);g.Clear(Color.White);g.DrawImage(image, r);g.Dispose();return bit;}catch (Exception){throw;}}/// <summary>/// 处理请求/// </summary>/// <param name="context"></param>public void ProcessRequest(HttpContext context){int w = 0, h = 0;int.TryParse(context.Request.QueryString["w"], out w);int.TryParse(context.Request.QueryString["h"], out h);Image image = Image.FromFile(context.Request.PhysicalPath);context.Response.ContentType = "image/jpeg";Bitmap bitMap = CreateThumbnail(image, w, h);bitMap.Save(context.Response.OutputStream, ImageFormat.Jpeg);image.Dispose();bitMap.Dispose();context.Response.End();}public bool IsReusable{get { return false; }}}

三 最后就是在Module和Handler的入口配置的,即如何将它们加入到当然网站中,我们采用web.config配置的方法

 <system.web><httpModules><!-- this is for Classic mode and Cassini --><add name="UrlRewriteModule" type="EntityFrameworks.Web.Core.HttpModules.UrlRewriteModule,EntityFrameworks.Web.Core" /></httpModules></system.web>

<system.webServer><modules runAllManagedModulesForAllRequests="true" ><!--This is for Integrated mode--><add name="UrlRewriteModule" type="EntityFrameworks.Web.Core.HttpModules.UrlRewriteModule,EntityFrameworks.Web.Core" /></modules><handlers><add name="ImageFunction1" path="*.jpg" verb="GET" type="EntityFrameworks.Web.Core.HttpHandlers.ImageScalingHandler,EntityFrameworks.Web.Core"  /><add name="ImageFunction2" path="*.png" verb="GET" type="EntityFrameworks.Web.Core.HttpHandlers.ImageScalingHandler,EntityFrameworks.Web.Core"  /><add name="ImageFunction3" path="*.gif" verb="GET" type="EntityFrameworks.Web.Core.HttpHandlers.ImageScalingHandler,EntityFrameworks.Web.Core"  /><add name="ImageFunction4" path="*.jpeg" verb="GET" type="EntityFrameworks.Web.Core.HttpHandlers.ImageScalingHandler,EntityFrameworks.Web.Core"  />
</handlers>

从上面的代码中,我们可以看到,对于modules来说,那有两种方式注入,一为IIS经典模式下的,另一种是IIS集成模式下的,我们需要分别进行配置。

可以看一下效果,成功的喜悦!

大叔框架集又一成功组件...

回到目录

转载于:https://www.cnblogs.com/lori/p/4755216.html

我心中的核心组件~HttpHandler和HttpModule实现图像的缩放与Url的重写相关推荐

  1. HttpHandler与HttpModule区别

    ASP.Net处理Http Request时,使用Pipeline(管道)方式,由各个HttpModule对请求进行处理,然后到达 HttpHandler,HttpHandler处理完之后,仍经过Pi ...

  2. 我心中的核心组件(可插拔的AOP)~第十三回 实现AOP的拦截组件Unity.Interception...

    说在前 本节主要说一下Unity家族里的拦截组件,对于方法拦截有很多组件提供,基本上每个Ioc组件都有对它的实现,如autofac,它主要用在orchard项目里,而castle也有以拦截的体现,相关 ...

  3. 选择HttpHandler还是HttpModule?

    阅读目录 开始 理解ASP.NET管线 理解HttpApplication 理解HttpHandler 理解HttpModule 三大对象的总结 案例演示 如何选择? 最近收到几个疑问:HttpHan ...

  4. asp.net Forums 之HttpHandler和HttpModule

    我们先说说IHttpHandler和IHttpModule这两个接口. 微软的解释为: IHttpHandler: 定义 ASP.NET 为使用自定义 HTTP 处理程序同步处理 HTTP Web 请 ...

  5. HttpHandler与HttpModule的用处与区别

    HttpHandler与HttpModule的用处与区别 问题1:什么是HttpHandler? 问题2:什么是HttpModule? 问题3:什么时候应该使用HttpHandler什么时候使用Htt ...

  6. HttpHandler和HttpModule 心得介绍

    HttpHandler和HttpModule--入门 ASP.Net处理Http Request时,使用Pipeline(管道)方式,由各个HttpModule对请求进行处理,然后到达 HttpHan ...

  7. 我心中的核心组件(可插拔的AOP)~大话开篇及目录

    我心中的核心组件(可插拔的AOP)~大话开篇及目录 http://www.cnblogs.com/lori/p/3247905.html 回到占占推荐博客索引 核心组件 我心中的核心组件,核心组件就是 ...

  8. ASP.NET内部原理(HttpHandler和HttpModule)

    [IT168 技术文档]在以前的ASP时候,当请求一个*.asp页面文件的时候,这个HTTP请求首先会被一个名为 inetinfo.exe进程所截获,这个进程实际上就是www服务.截获之后它会将这个请 ...

  9. 我心中的核心组件(可插拔的AOP)~分布式Session组件

    回到目录 对于目前的网站来说,为了满足高可用,高并发,高负载,一台WEB服务器已经远远不够用了,以后的WEB应用服务器应该是一种集群的环境,它们之间使用一些工具进行数据的同步,在由1台变成多台服务器时 ...

最新文章

  1. 防止IIS文件被下载方法
  2. dell服务器系统开机提示错误解决方法
  3. linux环境变量与文件查找
  4. sql注入一点小心得
  5. 界面设计--北京创享数码的设计案例(很不错的看看吧)
  6. day2-python工具的选择使用
  7. 为什么都建议学java而不是python-为什么都建议学Java而不是Python?两者有什么区别吗?...
  8. [整理]苹果审核被拒后,返回崩溃日志应该怎么分析处理
  9. Vim快捷键(四):Vim查找与替换
  10. 图书信息管理系统需求分析
  11. 吴声年度演讲全文:场景品牌,新商业的此时此刻
  12. Matlab中 regionprops和bwlabel的用法
  13. 电脑围棋领域的研究概述
  14. Windows 调色板
  15. 计算机无法识别建行网银盾,电脑无法识别建行网银盾怎么办
  16. ofo押金未退仍在自动续费上热搜,曾经的明星公司是怎么黄的?
  17. unity 使用像素实现墙面子弹留孔效果(给已有贴图模型叠加贴图)
  18. Google Gmail Oauth Client ID 认证指南
  19. pta一元多项式求导
  20. C++11的更新内容--auto--右值引用和移动构造--1114

热门文章

  1. python 怎么判断文件存在哪里_Python判断文件和文件夹是否存在的方法
  2. Spark- SparkSQL中 Row.getLong 出现NullPointerException错误的处理方法
  3. 个推基于Consul的配置管理
  4. Hibernate初探之单表映射——jar包的导入
  5. 服务器高并发下出现大量的time wait的解决办法
  6. 《构建之法》第8,9,10章
  7. jquery获取input值
  8. 根据中序和先序遍历创建一颗二叉树☆
  9. C++之使用IO库输入输出
  10. 关于maven依赖中的scope的作用和用法