·先创建ASP.NET项目,命名、添加MyHander.cs、Moduler.cs、添加img。
·在Moduler.cs中编写相关代码[继承IHttpModule与IRequiresSessionState接口]
public class Moduler : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += BRQ;
context.EndRequest += ERQ;
}
private void ERQ(object sender, EventArgs e)
{
HttpApplication ht = sender as HttpApplication;
string url = ht.Request.Url.ToString();
if (url.LastIndexOf(".jpg") == -1)
{
ht.Response.Write("多少有点害怕");
}
}
private void BRQ(object sender, EventArgs e)
{
HttpApplication ht = sender as HttpApplication;
string url = ht.Request.Url.ToString();
if (url.LastIndexOf(".jpg") == -1)
{
ht.Response.Write("多少有点害怕");
}
}
}
添加Web窗体
·在Web.config中写入
<system.webServer>
<modules>
<add name="m1" type="WebApplication4.Moduler"/>
</modules>
<handlers>
<add verb="" name="h1" type="WebApplication4.MyHandler" path="images/.jpg" />
</handlers>
</system.webServer>
·在Moduler.cs中编写
public class MyHandler : IHttpHandler
{
public bool IsReusable => false;
public void ProcessRequest(HttpContext context)
{
string filename = context.Request.PhysicalPath;
Bitmap bitmap = new Bitmap(filename);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawString("ABCD", new Font("微软雅黑", 18, FontStyle.Bold), Brushes.LightPink, new Point(10, 5));
graphics.Flush();
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
·最后在web窗体中写入Img路径及图片大小。
本次文档编写结束!