新建Web窗体CheckCode.aspx
如下CheckCode.aspx.cs的代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Design;
public partial class CheckCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.CreateCheckCodeImage(RndNum());
}
private string RndNum()
{
int number;
char code;
string checkcode = string.Empty;
System.Random random = new Random();
for (int i = 0; i < 4; i++)
{
number = random.Next();
if(number % 2== 0)
code = (char)('0' + (char)(number % 10));
else if(number % 3== 0)
code = (char)('a' + (char)(number % 26));
else
code = (char)('A' + (char)(number % 26));
checkcode += code.ToString();
}
Session["CheckCode"] = checkcode;
return checkcode;
}
private void CreateCheckCodeImage(string checkcode)
{
if (checkcode == null || checkcode.Trim() == string.Empty)
return;
System.Drawing.Bitmap Image = new System.Drawing.Bitmap((int)Math.Ceiling((checkcode.Length*12.5)),22);
Graphics G = Graphics.FromImage(Image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
G.Clear(Color.White);
//画图片的背景噪音线
for(int i= 0; i < 25; i++)
{
int X1 = random.Next(Image.Width);
int X2 = random.Next(Image.Width);
int Y1 = random.Next(Image.Height);
int Y2 = random.Next(Image.Height);
G.DrawLine(new Pen(Color.Silver),X1,Y1,X2,Y2);
}
Font FONT = new System.Drawing.Font("ARIAL",12,(System.Drawing.FontStyle.Bold|System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush Brush=new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0,Image.Width,Image.Height), Color.Blue,Color.DarkRed,1.2F,true);
G.DrawString(checkcode,FONT, Brush,2, 2);
//画图片的前景噪音点
for(int i = 0; i < 100; i++)
{
int x= random.Next(Image.Width);
int y= random.Next(Image.Height);
Image.SetPixel(x,y,Color.FromArgb(random.Next()));
}
//画图片的边框线
G.DrawRectangle(new Pen(Color.Silver),0,0,Image.Width-1,Image.Height-1);
System.IO.MemoryStream MS=new System.IO.MemoryStream();
Image.Save(MS,System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "IMAGE/GIF";
Response.BinaryWrite(MS.ToArray());
}
finally
{
G.Dispose();
Image.Dispose();
}
}
}
然后在登录页面login.aspx中:
<asp:ImageButton ID="YZM" ImageUrl="~/checkcode.aspx" runat="server" Height="26px" Style="z-index: 101;
left: 143px; position: absolute; top: 65px" Width="57px" />
<asp:LinkButton ID="CHANGECODE" runat="server" οnclick="CHANGECODE_CLICK" Style="z-index: 103; left: 141px;position: absolute; top: 99px" Font-Size="10pt" Width="81px">换一个验证码</asp:LinkButton>
<ASP:BUTTON ID="BUTTON1" RUNAT="SERVER" ONCLICK="BUTTON1_CLICK" TEXT="BUTTON" />
最后在login.aspx.cs中:
protected void CHANGECODE_CLICK(object sender, EventArgs e)
{
//什么都不用写
}
public void BUTTON1_CLICK(OBJECT SENDER, EVENTARGS E)
{
if (Session["CheckCode"].ToString().ToLower() != TextBox.Text.ToLower())//忽略大小写
{
//"验证码错误!
}
else
{
//验证码正确
}
}