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.