A simple and practical verification code

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;
Using system. Drawing. imaging;
Using system. IO;
Public partial class test: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
// 4-digit Verification Code
String str_validatecode = getrandomnumberstring (4 );
// The session used for verification
Session ["validatecode"] = str_validatecode;
Createimage (str_validatecode );
}
}
// Generate a random number string
Public String getrandomnumberstring (INT int_numberlength)
{
String str_number = string. empty;
Random therandomnumber = new random ();

For (INT int_index = 0; int_index <int_numberlength; int_index ++)
Str_number + = therandomnumber. Next (10). tostring ();

Return str_number;
}

Public color getrandomcolor ()
{
Random randomnum_first = new random (INT) datetime. Now. ticks );
// There is nothing to say about the random number of C #.
System. Threading. thread. Sleep (randomnum_first.next (50 ));
Random randomnum_sencond = new random (INT) datetime. Now. ticks );

// Try to generate a dark color for display on a white background
Int int_red = randomnum_first.next (256 );
Int int_green = randomnum_sencond.next (256 );
Int int_blue = (int_red + int_green> 400 )? 0: 400-int_red-int_green;
Int_blue = (int_blue & gt; 255 )? 255: int_blue;

return color. fromargb (int_red, int_green, int_blue);
}

Public void createimage (string str_validatecode)
{< br> int int_imagewidth = str_validatecode.length * 13;
random newrandom = new random ();
// figure height: 20px
bitmap thebitmap = new Bitmap (int_imagewidth, 20);
graphics thegraphics = graphics. fromimage (thebitmap);
// white background
thegraphics. clear (color. white);
// gray border
thegraphics. drawrectangle (new pen (color. lightgray, 1), 0, 0, int_imagewidth-1, 19);

// 10pt font
Font thefont = new font ("Arial", 10 );

For (INT int_index = 0; int_index <str_validatecode.length; int_index ++)
{
String str_char = str_validatecode.substring (int_index, 1 );
Brush newbrush = new solidbrush (getrandomcolor ());
Point thepos = new point (int_index * 13 + 1 + newrandom. Next (3), 1 + newrandom. Next (3 ));
Thegraphics. drawstring (str_char, thefont, newbrush, thepos );
}

// Send the generated image back to the client
Memorystream MS = new memorystream ();
Thebitmap. Save (MS, imageformat. PNG );

Response. clearcontent (); // You Need to output the image information to modify the HTTP header.
Response. contenttype = "image/PNG ";
Response. binarywrite (Ms. toarray ());
Thegraphics. Dispose ();
Thebitmap. Dispose ();
Response. End ();
}
}

webpage call: img src = " *. aspx " />

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.