带有背景噪音线的验证码
写登陆界面时,少不了要写验证码,下面是我整理的生成数字字母混合随机验证码并带有背景噪音线的。net代码。
首先,建立一个名为VerifyImage.aspx的web窗体,后台编写如下代码。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
public partial class Login_VerifyImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string Figure = Request.QueryString["pNum"].ToString();
int pW = 70;
int pH = 22;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms = GetImg(Figure, pW, pH);
Response.ClearContent();
Response.ContentType = "Last/Gif";
Response.BinaryWrite(ms.ToArray());
}
/// <summary>
/// 生成图像并获取二进制信息
/// </summary>
/// <param name="Figure">验证码字符串</param>
/// <param name="Width">图像宽度</param>
/// <param name="Height">图像高度</param>
/// <returns></returns>
public System.IO.MemoryStream GetImg(string Figure, int Width, int Height)
{
System.Drawing.Bitmap Last = new System.Drawing.Bitmap(Width, Height);
Graphics g = Graphics.FromImage(Last);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 8; i++)
{
int x1 = random.Next(Last.Width);
int x2 = random.Next(Last.Width);
int y1 = random.Next(Last.Height);
int y2 = random.Next(Last.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("宋体", 15, (System.Drawing.FontStyle.Bold));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, Last.Width, Last.Height), Color.Black, Color.DarkBlue, 1.2f, true);
g.DrawString(Figure, font, brush, 5, 2);
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = random.Next(Last.Width);
int y = random.Next(Last.Height);
Last.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, Last.Width - 1, Last.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
Last.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms;
}
finally
{
g.Dispose();
Last.Dispose();
}
}
}
然后,新名为建一个Login.aspx的登陆页面,拖拽一个img控件,一个textBox控件,一个button控件和一个LinkButton控件,Html代码如下:
html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
验证码:<img ID="MyImage" style="vertical-align:top" height="22" width="70" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="验证" onclick="Button1_Click" />
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">看不清,换一张?</asp:LinkButton>
</div>
</form>
</body>
</html>
,后天编写生成随机位数及验证码验证是否正确。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Login_LoginForm : System.Web.UI.Page
{
private static string strNum;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
strNum = CreateFigure(4);
MyImage.Src = "VerifyImage.aspx?pNum=" + strNum + "";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Equals(strNum))
{
//校验码正确
}
else
{
Response.Write("校验码不正确");
}
}
/// <summary>
/// 产生随即验证码
/// </summary>
/// <param name="BitNum">验证码位数</param>
/// <returns></returns>
public string CreateFigure(int BitNum)
{
string Base = "123456789abcdefghjklmnpqrstuvwxyz";
Random Rm = new Random();
string Figure = "";
for (int i = 0; i < BitNum; i++)
{
int m = Rm.Next(0, Base.Length - 1);
Figure += Base.Substring(m, 1);
}
return Figure;
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
strNum = CreateFigure(4);
MyImage.Src = "VerifyImage.aspx?pNum=" + strNum + "";
}
}