1. Test page: Default. aspx
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> ASP. NET Verification Code </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
Enter the verification code: <input id = "txtVI" type = "text" name = "view_id" runat = "server" maxlength = "5" size = "5"/>
<Asp: Image id = "Image1" runat = "server" ImageUrl = "ValidateCode. aspx" ImageAlign = "Middle"/>
<Asp: RequiredFieldValidator ID = "RequiredFieldValidator1" runat = "server" ErrorMessage = "You have not entered the verification code" ControlToValidate = "txtVI"> </asp: RequiredFieldValidator>
<Asp: Button ID = "Button1" runat = "server" Text = "Submit" OnClick = "button#click"/> </div>
</Form>
</Body>
</Html>
2. Test page Background: Default. aspx. cs
Using System;
Using System. Data;
Using System. Configuration;
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;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#click (object sender, EventArgs e)
{
String VNum;
VNum = Session ["VNum"]. ToString ();
Session. Abandon ();
ViewState ["VNum"] = VNum;
If (txtVI. Value = ViewState ["VNum"]. ToString ())
{
Response. Write ("<script> alert ('the verification code is entered correctly! '); </Script> ");
Return;
}
Else
{
// The verification code entered by VIWShow. Text = "is incorrect! ";
Response. Write ("<script> alert ('verification code input error! '); </Script> ");
Return;
}
}
}
3. Verification code generation page: ValidateCode. aspx
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "ValidateCode. aspx. cs" Inherits = "ValidateCode" %>
4. Generate the verification code page Background:
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;
/* Copyright all (c) 2005 ZhongFeng, http://blog.csdn.net/SW515 */
Public partial class ValidateCode: System. Web. UI. Page
{
Private void Page_Load (object sender, System. EventArgs e)
{
This. CreateCheckCodeImage (GenerateCheckCode ());
}
# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Load + = new System. EventHandler (this. Page_Load );
}
# Endregion
Private string GenerateCheckCode ()
{
Int number;
Char code;
String checkCode = String. Empty;
System. Random random = new Random ();
For (int I = 0; I <5; I ++)
{
Number = random. Next ();
If (number % 2 = 0)
Code = (char) ('0' + (char) (number % 10 ));
Else
Code = (char) ('A' + (char) (number % 26 ));
CheckCode + = code. ToString ();
}
Session ["VNum"] = 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 );
System. Drawing. Graphics g = Graphics. FromImage (image );
Try
{
// Generate a random Generator
Random random = new Random ();
// Clear the background color of the image
G. Clear (Color. White );
// Draw the background noise line of the image
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 );
// Foreground noise of the image
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 ()));
}
// Draw the border line of the image
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 ();
}
}
}