ASP. NET: A good Random verification code class

Source: Internet
Author: User
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. design;
Using System. drawing;

Public partial class checkcode: system. web. UI. page
{< br> protected void page_load ( Object sender, eventargs e)
{< br> This . createcheckcodeimage (rndnum ();
}

private string rndnum ()
{< br> int number;
char code;
string checkcode = string. empty;

System. Random random= NewRandom ();

For ( Int I =   0 ; I <   4 ; I ++ )
{
Number = Random. Next ();
Code = ( Char )( ' 0 '   + ( Char ) (Number %   10 )); // Number
// If (Number % 2 = 0)
// Code = (char) ('0' + (char) (Number % 10 )); // Number
// Else
// Code = (char) ('A' + (char) (Number % 26 )); // Letter
Checkcode + = Code. tostring ();
}
Response. Cookies. Add ( New Httpcookie ( " Checkcode " , 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 );
Graphics g = Graphics. fromimage (image );
Try
{
// Generate random Generator
Random random =   New Random ();
// Clear background color
G. Clear (color. White );
// Background noise 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 System. Drawing. Font ( " Arial " , 12 , (System. Drawing. fontstyle. Regular | System. Drawing. fontstyle. italic ));
System. Drawing. drawing2d. lineargradientbrush =   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
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 );
MS for system. Io. memorystream =   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 ();
}
}
}

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.