1,创建一个网站,并在网站上添加HttpHandler类,继承IHttpHandler与IRequirssSessionState接口
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace WebApplication2
{
public class handler : IHttpHandler,IRequiresSessionState
{
public bool IsReusable => false;
private Random RandomSeed = new Random();
public void ProcessRequest(HttpContext context)
{
string strWord = "23456789qwertyuiopasdfghjklzxcvbnm";
string NumStr = null;
for(int i = 0; i < 5; i++)
{
NumStr += strWord[RandomSeed.Next(0, strWord.Length)];
}
context.Session["vcode"] = NumStr.ToLower();
CreateImages(context, NumStr);
}
private void CreateImages(HttpContext context,string checkCode)
{
int iwidth = (int)(checkCode.Length * 13);
Bitmap image = new Bitmap(iwidth, 22);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
Random rand = new Random();
for(int i = 0; i < 50; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawRectangle(new Pen(Color.LightGray,0),x,y,1,1);
}
for(int i = 0; i < checkCode.Length; i++)
{
int cindex = rand.Next(7);
int findex = rand.Next(5);
Font f = new Font(font[findex], 10, FontStyle.Bold);
Brush b = new SolidBrush(c[cindex]);
int ii = 4;
if ((i + 1) % 2 == 0)
{
ii = 2;
}
g.DrawString(checkCode.Substring(i, 1), f, b, 2 + (i * 12), ii);
}
g.DrawRectangle(new Pen(ColorTranslator.FromHtml("#cccccc"), 0), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);
context.Response.ClearContent();
context.Response.ContentType = "images/gif";
context.Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
context.Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
}
}
2.添加一个web窗口,使用验证码实现验证功能
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>账号:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>验证码:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" imageUrl="~/images/2025cbe68b27ef7051689ee2bd164546.jpeg"/>
<asp:LinkButton ID="LinkButton1" runat="server">刷新</asp:LinkButton>
</td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" Text="提交" Style="margin-left:50%;margin-top:2%" OnClick="Button1_Click"/>
</div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</form>
</body>
3.后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string account = this.TextBox1.Text;
string password = this.TextBox2.Text;
string code = this.TextBox3.Text;
if (Session["vcode"].ToString() == code.ToLower())
{
if (account == "admin" && password == "123456")
{
Response.Redirect("index.aspx");
}
else
{
Label1.Text = "账号密码不正确";
}
}
else
{
Label1.Text = "验证码不正确";
}
}
}
}
4.水印功能
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class Class1 : 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("水印",new Font("宋体",20,FontStyle.Bold),Brushes.Red,new Point(1,1));
graphics.Flush();
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
5.在web.config中实现当请求图片资源时由HttpHandler类处理
<!--<system.webServer>
<handlers>
<add verb="*" path="image/*" type="WebApplication1.Class1" name="123"/>
</handlers>
</system.webServer>-->
这就是本次的全部内容感谢各位观看!