Asp. NET tries to make a verification code.

Source: Internet
Author: User

Create a new checkcode.aspx to display the verification code.

The important code is as follows:

The private string Generatecheckcode ()//generatecheckcode method generates a CAPTCHA string.

{
int num;
char code;
String Checkcode = String. Empty;
System.Random random = new Random ();

for (int i = 0; i < 4; i++)//Modify this 4 for a few to make the number of digits of the verification code, this time only test, so do not make a general effect.
{
num = random. Next ();
if (num% 2 = = 0)
{
Code = (char) (' 0 ' + (char) (num% 10));
}
Else
{
Code = (char) (' A ' + (char) (num% 26));

}
Checkcode + = code. ToString ();
}
RESPONSE.COOKIES.ADD (New HttpCookie ("Checkcode", Checkcode));
return checkcode;
}

Createcheckcodeimg This method calls the canvas.

private void Createcheckcodeimg (string checkcode)
{
if (Checkcode = = NULL | | Checkcode.trim () = = string. Empty)
return;
System.Drawing.Bitmap img = new System.Drawing.Bitmap ((int) math.ceiling ((Checkcode.length * 12.5)), 22);
Graphics g = Graphics.fromimage (IMG);
Try
{

Random random = new random ();//Generate Random
G.clear (color.white);//Empty picture background color
for (int i = 0; i < 2; i++)//Picture background noise Line
{
int x1 = random. Next (IMG. Width);
int x2 = random. Next (IMG. Width);
int y1 = random. Next (IMG. Height);
int y2 = random. Next (IMG. Height);
G.drawline (New Pen (Color.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font ("Arial", System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (new Rectangle (0, 0, IMG. Width, IMG. Height), Color.Blue, color.darkred, 1.2f, true);
g.drawstring (checkcode, Font, brush, 2, 2);

for (int i = 0; i < i++)//Picture foreground noise Point
{
int x = random. Next (IMG. Width);
int y = random. Next (IMG. Height);
Img. SetPixel (x, Y, Color.FromArgb (random. Next ()));
}
Draw a picture border line
G.drawrectangle (New Pen (Color.silver), 0, 0, IMG. Width-1, IMG. HEIGHT-1);
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Img. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
Response.clearcontent ();
Response.ContentType = "Image/gif";
Response.BinaryWrite (Ms. ToArray ());
}
Finally
{
G.dispose ();
Img. Dispose ();
}
}

How to invoke?

Add a picture in the appropriate location on the login page. </td>

Asp. NET tries to make a verification code.

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.