Reprint and create an ASP. NET Verification Code

Source: Internet
Author: User

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 >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.