ASP. NET generate verification code source code

Source: Internet
Author: User

The ASP. NET code for generating verification codes introduced in this article comes from the eping blog. It is quite simple and the content details are clearly stated. The implementation method is as follows:
Using system;
Using system. Data;
Using system. configuration;
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; // Add reference

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Call a custom method to draw a verification code
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 ();
}
}
}
Style = "cursor: hand; width: 76px; Height: 21px" onclick = "This. src = This. SRC + '? '"/>

Related Article

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.