CheckCode.cs
Copy Code code 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;
Using System.Drawing;
<summary>
Summary description of Checkcode
</summary>
public class Checkcode
{
Public Checkcode ()
{
//
TODO: Add constructor logic here
//
}
public static void DrawImage ()
{
Checkcode img = new Checkcode ();
httpcontext.current.session["Checkcode"] = img. Rndnum (4);
Img.checkcodes (httpcontext.current.session["Checkcode"). ToString ());
}
<summary>
Generate Validation Pictures
</summary>
<param name= "Checkcode" > Validation characters </param>
private void Checkcodes (string checkcode)
{
int iwidth = (int) (CHECKCODE.LENGTH * 13);
System.Drawing.Bitmap image = New System.Drawing.Bitmap (iwidth, 23);
Graphics g = graphics.fromimage (image);
G.clear (Color.White);
Define Color
Color[] C = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, COLOR.PURPL e};
Defining fonts
string[] Font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "Song Body"};
Random rand = new Random ();
Random output Noise Point
for (int i = 0; i < i++)
{
int x = rand. Next (image. Width);
int y = rand. Next (image. Height);
G.drawrectangle (New Pen (Color.lightgray, 0), x, Y, 1, 1);
}
Output CAPTCHA characters for different fonts and colors
for (int i = 0; i < checkcode.length; i++)
{
int cindex = rand. Next (7);
int findex = rand. Next (5);
Font f = new System.Drawing.Font (Font[findex], System.Drawing.FontStyle.Bold);
Brush B = new System.Drawing.SolidBrush (C[cindex]);
int II = 4;
if ((i + 1)% 2 = 0)
{
II = 2;
}
g.DrawString (Checkcode.substring (i, 1), F, B, 3 + (I *), ii);
}
Draw a border
G.drawrectangle (New Pen (Color.Black, 0), 0, 0, image. Width-1, image. HEIGHT-1);
Output to Browser
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Image. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);
HttpContext.Current.Response.ClearContent ();
Response.clearcontent ();
HttpContext.Current.Response.ContentType = "Image/jpeg";
HttpContext.Current.Response.BinaryWrite (Ms. ToArray ());
G.dispose ();
Image. Dispose ();
}
<summary>
Generate a random letter
</summary>
<param name= "Vcodenum" > number of letters generated </param>
<returns>string</returns>
private string rndnum (int vcodenum)
{
String Vchar = "0,1,2,3,4,5,6,7,8,9";
string[] Vcarray = Vchar.split (', ');
String vnum = ""; Because the string is very short, you don't have to StringBuilder.
int temp =-1; Record the last random number and try to avoid producing a few of the same random numbers
A simple algorithm is used to guarantee the difference of generating random numbers
Random rand = new Random ();
for (int i = 1; i < Vcodenum + 1; i++)
{
if (temp!=-1)
{
Rand = new Random (i * temp * unchecked ((int) DateTime.Now.Ticks));
}
int t = rand. Next (vcarray.length);
if (temp!=-1 && temp = t)
{
Return Rndnum (Vcodenum);
}
temp = t;
Vnum + = vcarray[t];
}
return vnum;
}
}
Then create a reference class page checkcode.aspx The foreground does not have to write anything, the background refers to the class we created DrawImage () method.
Copy Code code as follows:
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;
public partial class CheckCode:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Checkcode.drawimage ();
}
}
Below we can refer to the Checkcode.aspx page in the page that needs the verification code.
Front desk
Copy Code code as follows:
<asp:textbox id= "Validator" runat= "Server" width= "150px" ></asp:TextBox>
Style= "WIDTH:73PX; height:22px "align=" left "/>
<asp:imagebutton id= "Imgbtnlogin" runat= "Server" imageurl= "~/images/login.gif"
onclick= "Imgbtnlogin_click"/>
Background judgment
Copy Code code as follows:
protected void Imgbtnlogin_click (object sender, ImageClickEventArgs e)
{
if (this. validator.text==session["Checkcode"]. ToString ())
{
。。。。
}
Else
{
Response.Write (' <script>alert (' Authentication code input error, please re-enter! '); location= ' mumbervalidate.aspx ' </script> ');
Return
}
}
The above code please make appropriate changes according to the actual situation.