Protectedvoid Page_Load (Objectsender, EventArgs e) { string[] str =New string[4]; stringServercode =""; //Generate random GeneratorsRandom random =NewRandom (); for(inti =0; I <4; i++) {Str[i]= Random. Next (Ten). ToString (). Substring (0,1); } createcheckcodeimage (str); foreach(stringSinchstr) {Servercode+=s; } session["Servercode"] =Servercode; } Private voidCreatecheckcodeimage (string[] checkcode) { if(Checkcode = =NULL|| Checkcode.length <=0) return; System.Drawing.Bitmap Image=NewSystem.Drawing.Bitmap ((int) math.ceiling ((Checkcode.length *32.5)), -); System.Drawing.Graphics g=graphics.fromimage (image); Try{random random=NewRandom (); //clear the background color of the pictureg.clear (Color.White); //draw a picture of the background noise line for(inti =0; I < -; i++) { intX1 =Random. Next (image. Width); intx2 =Random. Next (image. Width); intY1 =Random. Next (image. Height); inty2 =Random. Next (image. Height); G.drawline (NewPen (color.silver), x1, y1, x2, y2); } //Define ColorColor[] C ={color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, Color.Purple}; //Defining Fonts string[] f = {"Verdana","Microsoft Sans Serif","Comic Sans MS","Arial","Song Body" }; for(intK =0; K <= Checkcode.length-1; k++) { intCIndex = random. Next (7); intFindex = random. Next (5); Font Drawfont=NewFont (F[findex], -, (System.Drawing.FontStyle.Bold)); SolidBrush Drawbrush=NewSolidBrush (C[cindex]); floatx =5.0F; floaty =0.0F; floatwidth =20.0F; floatHeight =25.0F; intSJX = random. Next (Ten); intSjy = random. Next (image. Height-(int) height); RectangleF DrawRect=NewRectangleF (x + SJX + (k * -), Y +sjy, width, height); StringFormat Drawformat=NewStringFormat (); Drawformat.alignment=Stringalignment.center; g.DrawString (Checkcode[k], Drawfont, Drawbrush, DrawRect, Drawformat); } //draw the foreground noise point of the picture for(inti =0; I < -; i++) { intx =Random. Next (image. Width); inty =Random. Next (image. Height); Image. SetPixel (x, Y, Color.FromArgb (random. Next ())); } //draw the border line of a pictureG.drawrectangle (NewPen (Color.silver),0,0, image. Width-1, image. Height-1); System.IO.MemoryStream Ms=NewSystem.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 (); } }
Page
<%@ Page language="C #"autoeventwireup="true"Codebehind="RegisterUser.aspx.cs"inherits="JackdeskNoteWebForm.User.RegisterUser"%><! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">"Server"> <title> User Registration </title> <script type="Text/javascript"Src="/formvalidator/jquery-1.4.1.min.js"></script> <script type="Text/javascript">function AA () {$ ("#image1"). attr ('src','.. /img.aspx?'+math.random ()); } </script>"Wrap"> <form id="Form2"runat="Server"> <div id="Container1"style="height:300px"> <fieldset style="Text-align:left"> <legend id="Legend1"> User Registration </legend> <table border="0"cellspacing="0"cellpadding="0"Width="550px"> <tr> <TDclass="Tdright">Verify the Picture:</td> <td> ".. /img.aspx"Id="Image1"alt=""onclick="AA ()"title="I can't see a change ."/> </td> <TDclass="Tdwidth"> <a href="Javascript:aa ()"> Can't see clearly </a> <span id="Span1"style="width:200px"></span> </td> </tr> <tr> <tdclass="Tdright">Verification Code:</td> <td> <asp:textbox id="Tbxcode"runat="Server"></asp:TextBox> </td> <TDclass="Tdwidth"> </td> </tr> </table> </fieldset> </div> </form> </div></body> Verify
string checkcode = session["servercode"]. ToString (); string getcode=TbxCode.Text.Trim (); if (getcode!=Checkcode) { MessageBox.Show (this" captcha error "); }
C # Implementation Verification code