Asp.net verification code generation class

Source: Internet
Author: User

Using System;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. Web. UI;
Using System. Drawing. Drawing2D;
Using System. IO;
Namespace Validate
{
/// <Summary>
/// The class that generates the verification code
/// </Summary>
Public class ValidateNumber
{
Public ValidateNumber ()
{
}
/// <Summary>
/// Maximum length of the Verification Code
/// </Summary>
Public int MaxLength
{
Get {return 10 ;}
}
/// <Summary>
/// Minimum length of the Verification Code
/// </Summary>
Public int MinLength
{
Get {return 1 ;}
}
/// <Summary>
/// Generate the verification code
/// </Summary>
/// <Param name = "length"> specify the length of the Verification Code </param>
/// <Returns> </returns>
Public string CreateValidateNumber (int length)
{
Int [] randMembers = new int [length];
Int [] validateNums = new int [length];
String validateNumberStr = "";
// Generate the starting Sequence Value
Int seekSeek = unchecked (int) DateTime. Now. Ticks );
Random seekRand = new Random (seekSeek );
Int beginSeek = (int) seekRand. Next (0, Int32.MaxValue-length * 10000 );
Int [] seeks = new int [length];
For (int I = 0; I <length; I ++)
{
BeginSeek ++ = 10000;
Seeks [I] = beginSeek;
}
// Generate random numbers
For (int I = 0; I <length; I ++)
{
Random rand = new Random (seeks [I]);
Int pownum = 1 * (int) Math. Pow (10, length );
RandMembers [I] = rand. Next (pownum, Int32.MaxValue );
}
// Extract random numbers
For (int I = 0; I <length; I ++)
{
String numStr = randMembers [I]. ToString ();
Int numLength = numStr. Length;
Random rand = new Random ();
Int numPosition = rand. Next (0, numLength-1 );
ValidateNums [I] = Int32.Parse (numStr. Substring (numPosition, 1 ));
}
// Generate the verification code
For (int I = 0; I <length; I ++)
{
ValidateNumberStr + = validateNums [I]. ToString ();
}
Return validateNumberStr;
}
/// <Summary>
/// Image of the Verification Code
/// </Summary>
/// <Param name = "containsPage"> page object to be output </param>
/// <Param name = "validateNum"> Verification Code </param>
Public void CreateValidateGraphic (Page containsPage, string validateNum)
{
Bitmap image = new Bitmap (int) Math. Ceiling (validateNum. Length * 12.5), 22 );
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 );
// Picture interference line
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
ContainsPage. Response. Clear ();
ContainsPage. Response. ContentType = "image/jpeg ";
ContainsPage. Response. BinaryWrite (stream. ToArray ());
}
Finally
{
G. Dispose ();
Image. Dispose ();
}
}
/// <Summary>
/// Obtain the length of the Verification Code Image
/// </Summary>
/// <Param name = "validateNumLength"> Verification code length </param>
/// <Returns> </returns>
Public static int GetImageWidth (int validateNumLength)
{
Return (int) (validateNumLength * 12.5 );
}
/// <Summary>
/// Obtain the height of the Verification Code
/// </Summary>
/// <Returns> </returns>
Public static double GetImageHeight ()
{
Return 22.5;
}
}
}

Related Article

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.