ASP. NET image verification code learning!

Source: Internet
Author: User

1. Create a New Validate. aspx and write the code in Validate. aspx. cs:

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Web;
Using System. Drawing;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Using System. Drawing. Imaging;
Using System. IO;

 

Public partial class Validate: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
This. CreateCheckCodeImage (RndNum ());

}
Private string RndNum ()
{
Int number;
Char code;
String checkCode = String. Empty;

System. Random random = new Random ();

For (int I = 0; I <4; I ++)
{
Number = random. Next ();
If (number % 2 = 0)
Code = (char) ('0' + (char) (number % 10 ));
Else
Code = (char) ('A' + (char) (number % 26 ));
CheckCode + = code. ToString ();
}

Session ["CheckCode"] = checkCode;
Return checkCode;
}
Private void CreateCheckCodeImage (string checkCode)
{
If (checkCode = null | checkCode. Trim () = String. Empty)
Return;
System. Drawing. Bitmap image = new System. Drawing. Bitmap (int) Math. Ceiling (checkCode. Length * 12.5), 22 );
Graphics g = Graphics. FromImage (image );
Try
{

Random random = new Random ();

G. Clear (Color. White );

For (int I = 0; I <25; 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. Silver), x1, y1, x2, y2 );
}

Font font = new System. Drawing. Font ("Arial", 12, (System. Drawing. FontStyle. Bold | System. Drawing. FontStyle. Italic ));
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 ++)
{
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 );
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 ();
}
}

}

 

2. Add this code on the page you want to add at the front-end: (I assume that the image verification code is added to Login. aspx on the logon page)

Img. src = img. src + '? ';
}
</Script>

 

3. Add the implementation code in the background Login. aspx. cs:

Reference ......

Public partial class Login: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{

}

  
Protected void btnLogin_Click (object sender, ImageClickEventArgs e)
{
User user = new User ();
User. LoginId = this.txt _ LoginId. Text. Trim ();
User. LoginPwd = this.txt _ LoginPwd. Text. Trim ();
If (this.txt Code. Text. ToLower () = Session ["CheckCode"]. ToString (). ToLower ())
{
Response. Redirect ("Index. aspx ");

Else
{
Session ["user"] = null;
This. Page. ClientScript. RegisterStartupScript (this. GetType (), "str", "<script> alert (\" Logon Failed !! \ ") </Script> ");
}
}
Else
{
This. Page. ClientScript. RegisterStartupScript (this. GetType (), "str", "<script> alert (\" Incorrect verification code !! \ ") </Script> ");
}

}

  
Protected void btn_Register_Click (object sender, ImageClickEventArgs e)
{
Response. Redirect ("Register. aspx ");
}
}

 

The final verification code is like this ~~

 

 

Of course, if you want to implement code on other pages, there will be some minor changes, but the basic code is like this ~~

Thanks ~~

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.