打开Visual Studio
新建Web窗体
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="Images/1.jeg" />
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />
</div>
简单做出验证码验证过程。
新建Images文件夹,添加handel类
public class MyHandles : IHttpHandler,IRequiresSessionState
{
//实现接口
public bool IsReusable => false;
public void ProcessRequest(HttpContext context)
{
//定义一个随机对象
Random random = new Random();
//产生随机数
int a = random.Next(0, 9);
int b = random.Next(0, 9);
int c = random.Next(0, 9);
int d = random.Next(0,9);
int e = random.Next(0, 9);
int f = random.Next(0, 9);
//建立一个Code装随机数
string code = "";
code = code + a + b + c + d + c + f;
context.Session["code"] = code;//将验证码放入Session作用域
//创建图
Bitmap bitmap = new Bitmap(120,30);
Graphics graphics = Graphics.FromImage(bitmap);
//绘制验证码
graphics.DrawString(code, new Font("微软雅黑", 12, FontStyle.Bold), Brushes.Red, new Point(2, 2));
graphics.Flush();
bitmap.Save(context.Response.OutputStream,ImageFormat.Jpeg);
}
在相关配置文件中添加
<system.webServer>
<handlers>
<add verb="*" name="h1" type="WebApplication4.MyHandles" path="Images/*"/>
</handlers>
</system.webServer>
运行代码
完成此案例
本次案例均为本人大二学年的课堂练习
如有雷同,纯属巧合