Verification code source code

Source: Internet
Author: User

First:

Using system. drawing;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Createcheckcodeimage (generatecheckcode ());
}

Private string generatecheckcode ()
{
// Create an integer variable
Int number;
// Create a variable
Char code;
// Create a string variable and initialize it as null
String checkcode = string. empty;
// Create a random object
Random random = new random ();
// Use the for loop to generate four numbers
For (INT I = 0; I <4; I ++)
{
// Generate a random number
Number = random. Next ();
// Convert the number to the numeric type
Code = (char) ('0' + (char) (Number % 10 ));

Checkcode + = code. tostring ();
}
// Add the generated random number to cookies
Response. Cookies. Add (New httpcookie ("checkcode", checkcode ));
// Return a string
Return checkcode;
}

Private void createcheckcodeimage (string checkcode)
{
// Judge whether the string is not equal to null or null
If (checkcode = NULL | checkcode. Trim () = string. Empty)
Return;
// Create a bitmap object
System. Drawing. bitmap image = new system. Drawing. Bitmap (INT) math. Ceiling (checkcode. length * 12.5), 22 );
// Create a graphics object
Graphics G = graphics. fromimage (image );

Try
{
// Generate a random Generator
Random random = new random ();

// Clear the background color of the image
G. Clear (color. White );

// Draw the background noise line of the image
For (INT I = 0; I <2; 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. Black), X1, Y1, X2, Y2 );
}

Font font = new system. Drawing. Font ("Arial", 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 );

// Foreground noise of the image
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 );

// Output the image to the page
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 ();
}
}
}

Second:

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Label1.text = randomnum (4); // generate a verification code
}
}
Public String randomnum (int n)
{

String strchar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T, U, V, W, X, Y, Z, A, B, C, D, E, F, G, h, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z ";
String [] vcarray = strchar. Split (',');
String vnum = "";
Int temp =-1; // record the last random value, and try to avoid several identical random numbers.
// Use a simple algorithm to ensure different random numbers are generated
Random Rand = new random ();
For (INT I = 1; I <n + 1; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * (INT) datetime. Now. ticks );
}
Int T = Rand. Next (61 );
If (temp! =-1 & temp = T)
{
Return randomnum (N );
}
Temp = T;
Vnum + = vcarray [T];
}
Return vnum; // return the generated random number.
}

Third:

Using system. drawing;
Using system. Drawing. imaging;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Session ["code"] = randnum (4 );
Validatecode (session ["code"]. tostring (), 40, 20, "", 10, "# ffffff ");
}
Private string randnum (INT vcodenum)
{
String vchar = ";
String [] vcarray = vchar. Split (','); // split it into an array
String vnum = "";
Int temp =-1; // record the previous random value. Avoid producing several identical random numbers.

Random Rand = new random ();
// Use a simple algorithm to ensure different random numbers are generated
For (INT I = 0; I <vcodenum; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * unchecked (INT) datetime. Now. ticks ));
}

Int T = Rand. Next (vcarray. Length-1 );
If (temp! =-1 & temp = T)
{
Return randnum (vcodenum );

}
Temp = T;
Vnum + = vcarray [T];
}
Return vnum;
}

Private void validatecode (string vnum, int W, int H, string font, int fontsize, string bgcolor)
{
Bitmap IMG = new Bitmap (W, H); // instance for generating the image
Graphics G = graphics. fromimage (IMG); // generate a new graphics object from the IMG object
G. Clear (colortranslator. fromhtml (bgcolor); // background color
Font F = new font (font, fontsize); // generate a font class instance
Solidbrush S = new solidbrush (color. Black); // generates an instance of the brush class
G. drawstring (vnum, F, S, 3, 3); // write vnum to the image
IMG. Save (response. outputstream, imageformat. JPEG); // Save the image as a JPEG image file to the stream.
Response. contenttype = "image/JPEG ";
// Reclaim Resources
G. Dispose ();
IMG. Dispose ();
Response. End ();
}

}

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.