Make a code in the Image verification code interface
1Verification Code: <asp:textbox id="TextBox1"runat="Server"></asp:textbox><asp:image id="Image1"Imageurl="yzm.aspx"runat="Server"/>2<asp:button id="Button1"runat="Server"text="Validation"/><asp:label id="Label1"runat="Server"text=""></asp:Label>3
The image control points to yzm.aspx
<asp:image id= "Image1" imageurl= "yzm.aspx" runat= "Server"/>
Create a yzm.aspx in the background to draw the code is as follows
1 //referencing a using System.Drawing namespace2 3 //Create a canvas4Bitmap map =NewBitmap ( -, -);5 //specify to draw on that canvas6Graphics g =graphics.fromimage (map);7 //random Number class8Random r =NewRandom ();9 //Draw Background ColorTen One //prepare the background color and put it in the collection. AList<color> CList =NewList<color>(); - CList. ADD (color.azure); - CList. ADD (Color.lightgray); the CList. ADD (color.cornflowerblue); - CList. ADD (color.slateblue); - CList. ADD (color.mediumblue); - CList. ADD (color.steelblue); + CList. ADD (color.darkgreen); - CList. ADD (color.greenyellow); + CList. ADD (color.slateblue); A //Draw Background Color atG.fillrectangle (NewSolidBrush (Clist[r.next (0, CList. Count)]),0,0, -, -); - - //Draw Interference Lines - for(inti =0;i<Ten; i++)//Loop Draw I line of interference - { - //Create a Pen object pen inPen pen=NewPen (NewSolidBrush (Clist[r.next (0, CList. Count)]), R.next (0,Ten)); - //Draw Interference Lines toG.drawline (pen, R.next (0, -), R.next (0, -), R.next (0, -), R.next (0, -)); + } - //Verification Code Draw content the stringSS ="ABCDEFHIJKLMNOPQRSTUVWXYZabxdefghijklmnopqrstuvwxyz1234567890"; * strings =""; $ for(inti =0; I <4; i++)//random four-digit verification codePanax Notoginseng { -S + = ss. Substring ((R.next (0Ss. Length)),1); the } +session["Yzm"] =s; A //the font being drawn theFont f =NewFont ("blackbody", -); + //Painted brushes -Brush B =NewSolidBrush (color.brown); $ //Start drawing Verification Code $g.DrawString (S, F, B,0,0); - - //Save the Drawn verification code the //Response.outputstream output Stream - //System.Drawing.Imaging.ImageFormat.Jpeg saved formatsWuyi map. Save (Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg); theResponse.End ();
Page display
Validation code method
//Registration Verification button click eventButton1.Click + =button1_click;
voidButton1_Click (Objectsender, EventArgs e) { stringT1 =TextBox1.Text; stringT2 = session["YZM"]. ToString (); if(t1. ToUpper () = =T2. ToUpper ()) {Label1.Text="Verify Success! "; } Else{Label1.Text="validation failed! "; } }
See not clear click Picture Replacement verification code with JS method
<script type="text/javascript"> var0; document.getElementById ("Image1"). onclick = function () { this. SetAttribute ("src""yzm.aspx?id= " + a); A+ +; } </script>
Web Image Verification Code verification