1, set up a website, add a page valitionno.aspx, and Default.aspx, create a new folder images. As shown in the figure:
2, in the Images folder to add the following 5 pictures: bg_0.jpg,bg_1.jpg,bg_2.jpg,bg_3.jpg,bg_4.jpg
Bg_0.jpg
Bg_1.jpg
Bg_2.jpg
Bg_3.jpg
Bg_4.jpg
3, valitionno.aspx not put any control, the code is as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "ValitionNo.aspx.cs" inherits= "Guestbook_valitionno"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
</div>
</form>
</body>
4, double-click the page valitionno.aspx into the ValitionNo.aspx.cs page, as follows to fill the code:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
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.Imaging;
Using System.Drawing.Drawing2D;
Using System.Drawing;
public partial class GuestBook_ValitionNo:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Random rd = new Random (); Create a random Number object
The following 4 lines produce a string consisting of 6 letters and numbers
String str = "ABCDEFGHIJKLMNOPQRSTURWXYZ0123456789 imagination is infinite";
String Yanzheng = "";
for (int i = 0; i < 6; i++)
{
Yanzheng = Yanzheng + str. Substring (Rd. Next (42), 1);
}
Verify code values are stored in the session for comparison
session["Yanzheng"] = Yanzheng;
The following three sentences, by randomly looking for an existing image to produce a canvas bitmap
String bgfilepath = Server.MapPath ("./images/bg_" + new Random (). Next (5) + ". jpg");//randomly find an image
System.Drawing.Image imgobj = System.Drawing.Image.FromFile (Bgfilepath);
Bitmap Newbitmap = new Bitmap (imgobj, 290, 80);//Create Bitmap Object
Graphics g = graphics.fromimage (Newbitmap);//Create a drawing surface from a bitmap object created above
SolidBrush brush = new SolidBrush (color.blue);//Set Brush color
Defines an array containing 10 fonts
String[] Ziti ={"Arial", "Verdana", "Comic Sans MS", "Impact", "Haettenschweiler", "Lucida Sans Unicode", "Garamond", "Co Urier New "," Book Antiqua "," Arial Narrow "};
By looping, drawing each character,
for (int a = 0; a < Yanzheng. Length; a++)
{
Font textfont = new Font (ZITI[RD. Next (9)], fontstyle.bold);//font random, font size 30, bold
Draws one character at a time, sets the font format, the brush color, the character relative to the canvas's x-coordinate, and the character relative to the canvas's y-coordinate
g.DrawString (Yanzheng. Substring (A, 1), Textfont, brush, + A * 36, 20);
}
Save the picture of the picture into the output stream
Newbitmap.save (Response.outputstream, imageformat.gif);
}
}
5, add a text box in the Default.aspx, Picture button, label label and Normal button, ImageButton picture path is ~/valitionno.aspx, not a picture. The code is as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
Verification code: <asp:textbox id= "TextBox1" runat= "Server" width= "117px" ></asp:TextBox>
<asp:imagebutton id= "ImageButton1" runat= "Server" imageurl= "~/valitionno.aspx"/>
<asp:linkbutton id= "LinkButton1" runat= "Server" causesvalidation= "False" > Change to a </asp:linkbutton><br/ >
<asp:label id= "Label1" runat= "Server" forecolor= "Red" ></asp:label><br/>
<asp:button id= "Button2" runat= "Server" text= "OK" onclick= "button2_click"/></div>
</form>
</body>
6, in the Default.aspx.cs code 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;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void button2_click (object sender, EventArgs e)
{
if (session["Yanzheng"]!= null && textbox1.text!= session["Yanzheng"]. ToString ())
{
Label1.Text = "Authentication code error. ";
Return
}
Else
{
Label1.Text = "Verify code is correct. ";
}
}
}
7, the operating effect is as follows:
If the point verification code picture or point to change a button, you can make the verification code randomly changed, the background random change, the number of alphanumeric characters randomly changed.