随机生成验证码
************************************************************1*************************************************
CreateImage.aspx.cs 如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace CreateImage
{
/// <summary>
/// CreateImage 的摘要说明。
/// </summary>
public class CreateImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string checkCode = CreateCode(4);
//用于验证
Session["CheckCode"] = checkCode;
CreateImages(checkCode);
}
/*产生验证码*/
public string CreateCode(int codeLength)
{
string so = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] strArr=so.Split(',');
string code = "";
Random rand=new Random();
for (int i = 0; i < codeLength; i++)
{
code+=strArr[rand.Next(0, strArr.Length)];
}
return code;
}
/*产生验证图片*/
public void CreateImages(string code)
{
Bitmap image = new Bitmap(60, 20);
Graphics g = Graphics.FromImage(image);
WebColorConverter ww=new WebColorConverter();
g.Clear((Color)ww.ConvertFromString("#FAE264"));
Random random = new Random();
//画图片的背景噪音线
for (int i = 0; i < 12; 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.LightGray), x1, y1, x2, y2);
}
Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(0,0,image.Width,image.Height),Color.Blue,Color.Gray,1.2f,true);
g.DrawString(code, font, brush, 0, 0);
//画图片的前景噪音点
for (int i = 0; i < 10; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.White);
}
//画图片的边框线
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());
g.Dispose();
image.Dispose();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
之后只需要在调用的时候 填入<img src="CreateImage.aspx" align="middle">就可以
例如:新建WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="CreateImage.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<img src="CreateImage.aspx" align="middle"><BR>
</form>
</body>
</html>
****************************************************2******************************************
IMAGE.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.DESIGN;
USING SYSTEM.DRAWING;
PUBLIC PARTIAL CLASS IMAGECODE : 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
CODE = (CHAR)('A' + (CHAR)(NUMBER % 26));
CHECKCODE += CODE.TOSTRING();
}
RESPONSE.COOKIES.ADD(NEW HTTPCOOKIE(YZMCODE, 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 RUNAT=SERVER IMAGEURL=IMAGECODE.ASPX />
<ASP:LINKBUTTON ID=CHANGECODE RUNAT=SERVER TEXT=看不清楚?换一个验证码 ONCLICK=CHANGECODE_CLICK />
<ASP:BUTTON ID=BUTTON1 RUNAT=SERVER ONCLICK=BUTTON1_CLICK TEXT=BUTTON /></DIV>
//OGIN.ASPX.CS中的代码
添加
//啥代码也不用写 点击“看不清楚?换一个验证码”以后就可以刷新验证码
PROTECTED VOID CHANGECODE_CLICK(OBJECT SENDER, EVENTARGS E)
{
}
//提交按钮代码这里是通过COOKIES来验证的
PROTECTED VOID BUTTON1_CLICK(OBJECT SENDER, EVENTARGS E)
{
IF (STRING.COMPARE(REQUEST.COOKIES[YZMCODE].VALUE, A.TEXT, TRUE) != 0)
{
RESPONSE.WRITE(<SCRIPT>ALERT('验证码错误!')</SCRIPT>);
}
ELSE
{ RESPONSE.WRITE(<SCRIPT>ALERT('验证吗正确!!!')</SCRIPT>); }
}