The verification code is a picture. We need to write a section of ,SRC in the foreground code to point to a page (validateimage.aspx).
Copy Code code as follows:
<script language= "JavaScript" >
function changeimg () {
$ ("#imgCheckNo"). attr ("src", "validateimage.aspx?r=" + getrandom (999));
}
function Getrandom (n) {return Math.floor (math.random () * n + 1)}
</script>
<div>
<span onclick= "changeimg ();" > Can't see clearly? Change a </span>
</div>
<div>
<div class= "Labelcss" > Verification Code:</div>
<div>
<asp:textbox id= "Tbxcheckno" runat= "Server" cssclass= "Tbxcss" ></asp:TextBox>
</div>
<div>
<asp:button id= "btnsubmit" runat= "Server" text= "Register Now" onclick= "btnSubmit_Click"/>
</div>
Validateimage.aspx is used to produce the captcha picture and to save the code value of the CAPTCHA to the cookie.
The code is as follows:
Copy Code code as follows:
public partial class ValidateImage:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
This. Createcheckcodeimage (Rndnum ());
}
<summary> Generate Verification Code
</summary>
<returns></returns>
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 ();
}
RESPONSE.COOKIES.ADD (New HttpCookie ("Yzmcode", Checkcode));
return checkcode;
}
<summary> generate a verification code GIF picture to the page
</summary>
<param name= "Checkcode" ></param>
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
{
Generate a random generator
Random Random = new Random ();
Empty picture background color
G.clear (Color.White);
Background noise line for painting pictures
for (int i = 0; i < 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), (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);
Picture of the foreground noise point
for (int i = 0; i < i++)
{
int x = random. Next (image. Width);
int y = random. Next (image. Height);
Image. SetPixel (x, Y, Color.FromArgb) (random. Next ()));
}
Draw a picture's border line
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 ();
}
}
}
The last is the background event to determine whether the current verification code is correct.
The code is as follows:
Copy Code code as follows:
#region Page Control Events
protected void btnSubmit_Click (object sender, EventArgs e)
{
if (String.Compare (request.cookies["Yzmcode"). Value, Tbxcheckno.text, True)!= 0)
{
Response.Write (' <script>alert (' captcha error! ') </script> ");
}
Else
{
Response.Write (' <script>alert (' Verify right!!! ') </script> ");
}
}
#endregion