今天我们学习EntityFramework的制作方法
首先我们要点击选择创建ASP.NET(Framework)后
我们需要创建一个Web项目 里面要为空
当你创建完之后我们就需要
在解决方案里面加入两个类库
一个类库是:MyHandler
另一个为 :MyModule
当你创建好这两个类库是这样的:
然后我们需要在类库里面加入我们要编写的代码
首先我们编写MyHandler 记住当我们在编写 MyHandler代码时我们需要让他实现接口如下代码:
public class MyHandler : IHttpHandler,IRequiresSessionState
当你实现接口后我们将进行编写MyHandler 代码了
如下代码:
public class MyHandler : IHttpHandler,IRequiresSessionState
{
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("跟我比个✌", new Font("微软雅黑", 18, FontStyle.Bold), Brushes.Red, new Point(100, 100));
清除画笔
//graphics.Flush();
将图片存为输出流
//bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//验证
//创建一个画板,把获取到的图片放到画板上
Bitmap bitmap = new Bitmap(200, 100);
//创建画笔,准备在画板是绘画
Graphics graphics = Graphics.FromImage(bitmap);
//水印图片放置的位置,用画板的宽高减去水印图片的宽高看水印图片位置是否合适不合适自己调节
graphics.DrawString("ABCD", new Font("微软雅黑", 30, FontStyle.Bold), Brushes.Red, new Point(10, 2));
graphics.DrawLine(new Pen(Brushes.Blue), new Point(0, 0), new Point(30, 30));
//清除画笔
graphics.Flush();
//将图片存为输出流
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
当你把这些写完之后MyHandler 里面的代码我们就写完了现在我们将进行编写MyModule的代码、在此之前我们还是要实现MyModule的接口如下代码:
public class MyModule : IHttpModule
当我们接口完成后我们将进行MyModule内部的代码编写
如下代码:
public class MyModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
context.EndRequest += Context_EndRequest;
}
private void Context_BeginRequest(object sender, EventArgs e)
{
HttpApplication context = sender as HttpApplication;
string url = context.Request.Url.ToString();
if (url.LastIndexOf(".jpg")==-1)
context.Response.Write("Context_BeginRequest");
}
private void Context_EndRequest(object sender, EventArgs e)
{
HttpApplication context = sender as HttpApplication;
context.Response.Write("Context_EndRequest");
}
}
当我们这些都编写完之后我们的两个类库的代码就写完了
现在我们需要找到 Web.config 在这里面编写我们需要的代码
如下图:
点击去后我们需要编写代码如下代码:
<system.webServer>
<modules>
<add name="y1" type="FiveTask.MyModule" />
</modules>
<handlers>
<add verb="*" path="img/*" type="FiveTask.MyHandler" name="x1"/>
</handlers>
</system.webServer>
这些我们完成后我们就将要进行开始实行对程序的运行了
首先我们需要在Web里面添加新建项 为Web窗体
如下图:
然后我们需要进行编写添加一个图片也可以不添加因为我们的验证码是我们在MyHandler里面就写完了的
现在我们来看看我们运行的结果;
这么简单的你学会了吗、少年?