CheckCode.cs
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>///Checkcode Summary description///</summary> public class Checkcode {public Checkcode () {//////TODO: Here Add constructor logic//} public static void DrawImage () {checkcode img = new Checkcode (); httpcontext.current.session["Checkcode"] = img. Rndnum (4); Img.checkcodes (httpcontext.current.session["Checkcode"). ToString ()); }///<summary>///Generate verification images///</summary>//<param name= "Checkcode" > Verify characters </param> private void Ch Eckcodes (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); Definition color color[] c = {color.black, color.red, Color.darkblue, Color.green, Color.orange,Color.brown, Color.darkcyan, color.purple}; Define font string[] Font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "Song Body"}; Random rand = new Random (); Random output noise 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 the 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], ten, 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 random letters////</summary>//<param name= "Vcodenum" > Generate letters </param>//<ret Urns>string</returns> private string Rndnum (int vcodenum) {string Vchar = "0,1,2,3,4,5,6,7,8,9"; string[] VcArr ay = vchar.split (', '); String vnum = ""; Because the string is very short, you do not have to StringBuilder int temp =-1; Record the last random value and try to avoid producing several random numbers as much as possible.//Adopt A simple algorithm to guarantee the generation of random numbers with the different random rand = new Random (); for (int i = 1; i < Vcodenum + 1; i++) {if (temp! =-1) {rand = new Random (i * temp * unchecked ((int) DateTime.Now.Tic KS)); } int t = rand. Next (vcarray.length); if (temp! =-1 && temp = = t) {return rndnum (vcodenum);} temp = t; Vnum + = vcarray[t]; } return vnum; } }
Then set up a page referencing the class checkcode.aspx the front desk does not have to write something, the background refers to the class we created DrawImage () method.
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.dra Wimage (); } }
Below we can refer to the Checkcode.aspx page in the page that needs verification code.
Front desk
<asp:textbox id= "Validator" runat= "Server" width= "150px" ></asp:TextBox> <asp:imagebutton id=" imgbtnlogin "runat=" Server "imageurl=" ~/images/login.gif " onclick= "Imgbtnlogin_click"/>
Background judgment
protected void Imgbtnlogin_click (object sender, ImageClickEventArgs e) {if (this. validator.text==session["Checkcode"]. ToString ()) {//.... } else {Response.Write ("<script>alert (' captcha input error, please re-enter! '); location= ' mumbervalidate.aspx ' </script> '); Return } }
Please modify the above code according to the actual situation.