Verification Code
/*
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. Drawing. imaging;
Using system. Drawing. text;
*/
// Create a bitmap object
Bitmap newbitmap = new Bitmap (36, 16, pixelformat. format32bppargb );
// Create a drawing surface based on the previously created bitmap object
Graphics G = graphics. fromimage (newbitmap );
// Create a font object
Font textfont = new font ("Times New Roman", 10 );
// Create a rectanglef structure to specify a region
Rectanglef rectangle = new rectanglef (0, 0, 36, 16 );
// Create a random number object
Random RD = new random ();
// Obtain a random number
Int valationno = 1000 + RD. Next (9000 );
// Fill the area of the rectangle specified by the rectanglef structure with the specified color
G. fillrectangle (New solidbrush (color. burlywood), rectangle );
// Fill in the rectangle filled above with the random number generated above
G. drawstring (valationno. tostring (), textfont, new solidbrush (color. Blue), rectangle );
// Save the created bitmap to the specified path
// Method 1
// Newbitmap. Save (server. mappath ("IMG") + "\ vaimg.gif", imageformat. GIF );
// Method 2
Response. contenttype = "image/JPEG ";
Newbitmap. Save (response. outputstream, imageformat. JPEG );
/// Release the occupied Resources
Newbitmap. Dispose ();
11. Chinese Verification Code
/*
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. text;
*/
Public static object [] createregioncode (INT strlength)
{
String [] rbase = new string [16] {"0", "1", "2", "3", "4", "5", "6 ", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; // set
A String Array stores the elements of Chinese character encoding.
Random RND = new random ();
Object [] bytes = new object [strlength]; // defines an object array
For (INT I = 0; I <strlength; I ++)
{
// Code 1st bits
Int R1 = RND. Next (11,14 );
String str_r1 = rbase [R1]. Trim ();
// Code 2nd bits
RND = new random (R1 * unchecked (INT) datetime. Now. ticks) + I); // Replace the seed of the random number generator to avoid duplication.
Value
Int R2;
If (r1 = 13)
{R2 = RND. Next (0, 7 );}
Else
{R2 = RND. Next (0, 16 );}
String str_r2 = rbase [R2]. Trim ();
// Code 3rd bits
RND = new random (R2 * unchecked (INT) datetime. Now. ticks) + I );
Int R3 = RND. Next (10, 16 );
String str_r3 = rbase [R3]. Trim ();
// Code 4th bits
RND = new random (R3 * unchecked (INT) datetime. Now. ticks) + I );
Int R4;
If (R3 = 10)
{R4 = RND. Next (1, 16 );}
Else if (R3 = 15)
{R4 = RND. Next (0, 15 );}
Else
{R4 = RND. Next (0, 16 );}
String str_r4 = rbase [R4]. Trim ();
// Define the random Chinese character location code generated by storing two byte Variables
Byte byte1 = convert. tobyte (str_r1 + str_r2, 16 );
Byte byte2 = convert. tobyte (str_r3 + str_r4, 16 );
Byte [] str_r = new byte [] {byte1, byte2}; // store the two byte variables in the byte array
Bytes. setvalue (str_r, I); // put the byte array of A Generated Chinese character into the object Array
}
Return bytes;
}
Private void createimage (string checkcode)
{
If (checkcode = NULL | checkcode. Trim () = string. Empty)
Return;
System. Drawing. bitmap image = new system. Drawing. Bitmap (INT) math. Ceiling (checkcode. length * 21 )),
22 );
Graphics G = graphics. fromimage (image );
Try
{
Random random = new random (); // generate a random Generator
G. Clear (color. White); // clear the background color of the image.
For (INT I = 0; I <20; I ++) // draws the background noise line of the image.
{
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 ("", 12, (system. Drawing. fontstyle. Bold ));
System. Drawing. drawing2d. lineargradientbrush brush = 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 );
For (INT I = 0; I <100; I ++) // foreground noise of the picture
{
Int x = random. Next (image. width );
Int y = random. Next (image. Height );
Image. setpixel (X, Y, color. fromargb (random. Next ()));
}
G. drawrectangle (new pen (color. Silver), 0, 0, image. Width-1, image. Height-1); // draw an image edge
Frame line
System. Io. memorystream MS = 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 ();}
}
// Place the user here Code To initialize the page
Encoding GB = encoding. getencoding ("gb2312 ");
Object [] bytes = createregioncode (4); // call the function to generate four random Chinese character codes
// Decodes Chinese Characters Based on the byte array encoded by Chinese Characters
String str1 = GB. getstring (byte []) convert. changetype (Bytes [0], typeof (byte []);
String str2 = GB. getstring (byte []) convert. changetype (Bytes [1], typeof (byte []);
String str3 = GB. getstring (byte []) convert. changetype (Bytes [2], typeof (byte []);
String str4 = GB. getstring (byte []) convert. changetype (Bytes [3], typeof (byte []);
Session ["checkcode"] = str1 + str2 + str3 + str4; // compare with the entered Verification Code
Createimage (str1 + str2 + str3 + str4 );