. Net verification code generation class

Source: Internet
Author: User

1. First, write a verification code class.

Validatenumber

 

 

Using system;
Using system. Data;
Using system. configuration;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. IO;
Using system. Drawing. imaging;

/// <Summary>
/// Summary of validatenumber
/// Production verification code class
/// </Summary>
Public class validatenumber
{
Public validatenumber ()
{
 
}
// Length of the production verification code
Public int maxlength
{
Get {return 10 ;}
}
Public int minlength
{
Get {return 1 ;}
}
/// <Summary>
/// Generate the verification code
/// </Summary>
/// <Param name = "length"> specify its length </param>
/// <Returns> </returns>
Public String createvalidatenumber (INT length)
{
String validatememberstr = "";
// Declare an integer array to store random numbers
Int [] Rand = new int [length];
//////////////////////////////////////// ///// Declare an array to obtain a random number

Int [] validate = new int [length];
// The sequence value that generates a random number: time-based

Int seek = unchecked (INT) datetime. Now. ticks );
// Number of time-generated Sequences
Random seekrand = new random (seek );

// Obtain the random number of a range class
Int BS = (INT) seekrand. Next (0, int32.maxvalue-length * 10000 );

######## Redeclare an array to store random numbers
Int [] Ss = new int [length];
For (INT I = 0; I <length; I ++)
{
BS + = 10000;
Ss [I] = BS;
}
// Generate random numbers
For (INT I = 0; I <length; I ++)
{
Random rands = new random (ss [I]);
Int pownum = 1 * (INT) math. Pow (10, length );
Rand [I] = rands. Next (pownum, int32.maxvalue );
}
// Extract numbers
For (INT I = 0; I <length; I ++)
{
String numstr = Rand [I]. tostring ();
Int num = numstr. length;
Random randh = new random ();
Int numposition = randh. Next (0, num-1 );
// Give the second array
Validate [I] = int32.parse (numstr. substring (numposition, 1 ));
}
// Generate the verification code
For (INT I = 0; I <length; I ++)
{
// Generate a verification code in the random number array.
Validatememberstr + = validate [I]. tostring ();

}
Return validatememberstr;
}
// Create a verification code image and output it on the page
Public void createvalidategraphic (Page C, string validatenum)
{
Bitmap image = new Bitmap (INT) math. Ceiling (validatenum. length * 12.5), 22 );
Graphics G = graphics. fromimage (image );
// Generate the instant Generator
Random random = new random ();
// Clear the background color of the image
G. Clear (color. White );
// Draw the drying 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 font ("Arial", 12, (fontstyle. Bold | fontstyle. italic ));
Lineargradientbrush brush = new lineargradientbrush (New rectangle (0, 0, image. Width, image. Height ),
Color. Blue, color. darkred, 1.2f, true );
G. drawstring (validatenum, Font, brush, 3, 2 );
// Foreground interference points
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 );
// Save image data
Memorystream stream = new memorystream ();
Image. Save (stream, imageformat. JPEG );
// Output image
C. Response. Clear ();
C. Response. contenttype = "image/JPEG ";
C. Response. binarywrite (stream. toarray ());
}
// Verification code length
Public static int getimagewidth (INT validatenumlength)
{
Return (INT) (validatenumlength * 12.5 );
}
Public static double getimageheight ()
{
Return 22.5;
}
}
// Create a page

Write in the page

// Instantiate the Verification Code Class

Validatenumber S = new validatenumber ();

// Get a verification code number with a specified length. Create a verification code number.
String STR = S. createvalidatenumber (4 );

// Save the verification code in seesion
Session ["vnmber"] = STR;

// Create an image of the Verification Code
S. createvalidategraphic (this, STR );

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.