Then the verification code is implemented (according to Han xianlong's Code)

Source: Internet
Author: User

Protected void page_load (Object sender, eventargs E)
{
Encoding GB = encoding. getencoding ("gb2312 ");
 
// Call the function to generate four random numbers.
Object [] bytes = createregioncode (4 );
 

String str1 = bytes [0]. tostring ();
String str2 = bytes [1]. tostring ();
String str3 = bytes [2]. tostring ();
String str4 = bytes [3]. tostring ();
 
// Output Console

// Response. Write (str1 + str2 + str3 + str4 );
// Return;

String SRS = str1 + str2 + str3 + str4;
Bitmap srbmp = srbitmap (SRS );
System. Io. memorystream SRMs = new system. Io. memorystream ();
Srbmp. Save (SRMs, system. Drawing. imaging. imageformat. GIF );
Response. clearcontent ();
Response. contenttype = "image/GIF ";
Response. binarywrite (SRMs. toarray ());
Srbmp. Dispose ();

}

/// <Summary>
/// Return the bitmap object based on the specified parameter
/// Reference:
/// Using system. drawing;
/// Call example:
/// Eg1. Save it as an image file
/// Bitmap srbmp = srbitmap (SRS );
/// Srbmp. Save (directory. getcurrentdirectory () + "\ srs.gif", system. Drawing. imaging. imageformat. GIF );
/// Srbmp. Dispose ();
/// Eg2. The webpage is called as follows:
/// Bitmap srbmp = srbitmap (SRS );
/// System. Io. memorystream SRMs = new system. Io. memorystream ();
/// Srbmp. Save (SRMs, system. Drawing. imaging. imageformat. GIF );
/// Response. clearcontent ();
/// Response. contenttype = "image/GIF ";
/// Response. binarywrite (SRMs. toarray ());
/// Srbmp. Dispose ();
/// </Summary>
/// <Param name = "SRS"> </param>
/// <Returns> </returns>

Public static bitmap srbitmap (string SRs)
{
// Define the image Bending Angle
Int srseedangle = 40;
// Define the image
Bitmap srbmp = new Bitmap (SRS. length * 20, 30 );
// Draw
Graphics srgraph = graphics. fromimage (srbmp );
// Clear the image
Srgraph. Clear (color. aliceblue );
// Draw a border for the image
Srgraph. drawrectangle (new pen (color. Black, 0), 0, 0, srbmp. Width-1, srbmp. Height-1 );
// Define the instant count
Random srrandom = new random ();
// Define the paint brush
Pen srpen = new pen (color. lightblue, 0 );
// Draw Noise
For (INT I = 0; I <100; I ++)
{
Srgraph. drawrectangle (srpen, srrandom. Next (1, srbmp. Width-2), srrandom. Next (1, srbmp. Height-2), 1, 1 );
}
// Convert a string into a character array
Char [] srchars = SRS. tochararray ();
// Archive text
Stringformat srformat = new stringformat (stringformatflags. noclip );
// Set the vertical center of Text
Srformat. Alignment = stringalignment. Center;
// Set the text to center horizontally
Srformat. linealignment = stringalignment. Center;
// Define the font color
Color [] srcolors = {color. Black, color. Red, color. darkblue, color. Blue, color. Orange, color. Brown, color. darkcyan, color. Purple };
// Define the font
String [] srfonts = {"verdana", "Microsoft sans serif", "Comic Sans MS", "Arial", ""};
// Draw each character cyclically
For (INT I = 0, j = srchars. length; I <j; I ++)
{
// Define the font parameters as the font style, font size, and font shape.

Font srfont = new font (srfonts [srrandom. Next (5)], 12, fontstyle. Regular );
// Fill the image
Brush srbrush = new solidbrush (srcolors [srrandom. Next (7)]);
// Define coordinates
Point srpoint = new point (16, 16 );
// Define the skew angle
Float srangle = srrandom. Next (-srseedangle, srseedangle );
// Skew
Srgraph. translatetransform (srpoint. X, srpoint. y );
Srgraph. rotatetransform (srangle );
// Fill in characters
Srgraph. drawstring (srchars [I]. tostring (), srfont, srbrush, 1, 1, srformat );
// Return to normal
Srgraph. rotatetransform (-srangle );
Srgraph. translatetransform (2,-srpoint. y );
}
Srgraph. Dispose ();
Return srbmp;
}

 

Public static object [] createregioncode (INT strlength)
{

 


String [] rbase = new string [10] {"0", "1", "2", "3", "4", "5", "6 ", "7", "8", "9 "};
Random RND = new random ();

// Define an object array

Object [] bytes = new object [strlength];

For (INT I = 0; I <strlength; I ++)
{
Int R1 = RND. Next (0, 10 );
String str_r1 = rbase [R1]. Trim ();
RND = new random (R1 * unchecked (INT) datetime. Now. ticks + I); // Replace the seed of the random number generator to avoid repeated values.

// Define the random digit location code generated by storing two byte Variables
Byte byte1 = convert. tobyte (str_r1, 16 );

// Put the byte array of the generated Chinese character into the object Array
Bytes. setvalue (byte1, I );

 

 

 

 

}

Return bytes;

}

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.