Digital Verification Code

Source: Internet
Author: User

// Validatecode. aspx. CS
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. Drawing. imaging;
Using system. configuration;
Namespace yanzhengma
{
/// <Summary>
/// Summary of validatecode.
/// </Summary>
Public class validatecode: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
{
// Read the string length from the configuration file
Int num = convert. toint32 (configurationsettings. etettings ["num"]);
String str_validatecode = getrandomnumberstring (Num );
// The session used for verification
Session ["validatecode"] = str_validatecode;
Createimage (str_validatecode );
}
}

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;
}
// Generate random colors
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 ();
// A 20px bitmap object with a height of 20 PX is used to process images defined by pixel data.
bitmap thebitmap = new Bitmap (int_imagewidth, 20);
// graphics. fromimage creates a new graphics object.
graphics thegraphics = graphics. fromimage (thebitmap);
// white background
thegraphics. clear (color. white);
// The gray border thegraphics. drawrectangle draws a rectangular pen (paint brush color, paint width)
// drawrectangle (paint brush, X, Y, width, heigth)
thegraphics. drawrectangle (new pen (color. lightgray, 1), 0, 0, int_imagewidth-1, 19);
// font of 10pt
font thefont = new font ("Arial ", 10);

For (INT int_index = 0; int_index <str_validatecode.length; int_index ++)
{
// Substring (1, j) Take characters whose length is J from I
String str_char = str_validatecode.substring (int_index, 1 );
// Call the random returned color Method
Brush newbrush = new solidbrush (getrandomcolor ());
// Point (I, j) I horizontal position of the point, vertical position of the Point J
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); // save to the specified stream in the specified format

response. clearcontent (); // You Need to output the image information to modify the HTTP header
response. contenttype = "image/PNG";
response. binarywrite (Ms. toarray (); // write a binary string to the HTTP output stream
thegraphics. dispose (); // release all resources used by this graphics object.
thebitmap. Dispose ();
response. End ();
}

# Generated by region web Form DesignerCode
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion
}
}

// Add the following code to the page for verification:
Private void cmdsubmit_click (Object sender, system. eventargs E)
{
If (txtnum. Text = session ["validatecode"]. tostring ())
{
Response. Write ("OK ");
}
Else
{
Response. Write ("wrong ");
}
}

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.