Http://www.cnblogs.com/jianjialin/archive/2009/02/05.html
Verification Code Aspx
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. imaging;
Using System. Drawing. drawing2d;
Using System. drawing;
// Download this source code from www.51aspx.com (51aspx.com)
Namespace Complexvalide
{
Public Partial Class Validcode: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
Random Rd = New Random (); // Create random number object
// The following four lines generate a string consisting of 6 letters and numbers
String Str = " Abcdefghijklmnopqrsturwxyz0123456789 " ;
String My51aspx = "" ;
For ( Int I = 0 ; I < 6 ; I ++ )
{
My51aspx = My51aspx + Str. substring (RD. Next ( 36 ), 1 );
}
// The verification code value is stored in the session for comparison.
Session [ " Valid " ] = My51aspx;
// In the following three sentences, a canvas bitmap is generated by randomly finding an existing image.
String Bgfilepath = Server. mappath ( " . \ Images \ BG " + New Random (). Next ( 5 ) + " . Jpg " ); // Randomly find an image
System. Drawing. Image imgobj = System. Drawing. image. fromfile (bgfilepath );
Bitmap newbitmap = New Bitmap (imgobj, 290 , 80 ); // Create a bitmap object
Graphics g = Graphics. fromimage (newbitmap ); // Create a drawing surface based on the previously created bitmap object
Solidbrush brush = New Solidbrush (color. Black ); // Set paint color
// Define an array containing 10 Fonts
String [] fontfamily = { " Arial " , " Verdana " , " Comic Sans MS " , " Impact " , " Haettenschweiler " , " Lucida sans Unicode " , " Garamond " , " Courier New " , " Book antiqua " , " Arial narrow " };
// Draw each character in a loop,
For ( Int A = 0 ; < My51aspx. length; ++ )
{
Font textfont = New Font (fontfamily [RD. Next ( 9 )], 30 , Fontstyle. Bold ); // Random font, 30 characters in size, bold
// Draw one character at a time, set the font format, paint color, X coordinate of the character relative to the canvas, and Y coordinate of the character relative to the canvas
G. drawstring (my51aspx. substring (, 1 ), Textfont, brush, 40 + A * 36 , 20 );
}
//Save the image to the output stream.
Newbitmap. Save (response. outputstream, imageformat. GIF );
}
}
}
Qiantai
< Script Type = "Text/JavaScript" >
Function Showvalidimage (){
VaR Numkey = Math. Random ();
Document. getelementbyid ( " Imgrandom " ). SRC = " /Validcode. aspx? Numkey = " + Numkey;
}
</ Script >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div >
< IMG ID = "Imgrandom" ALT = "Cannot see clearly? Click Change" Onclick = "Showvalidimage ()" SRC = "/Validcode. aspx? " Title = "Cannot see clearly? Click Change" />
</ Div >
</ Form >
</ Body >