<div> <asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox> <%--textbox-wait for input verification code--% > <asp:image id= "Image1" runat= "Server"/> <%--image-show captcha picture--%> <asp:button id= "Button1" runat= "Server" text= "Commit Validation"/> <%--button-submit for validation--%> <asp:label id= "Label1" runat= "Server" text= "Verify ..." ></asp:Label> <%--label-Show validation results--%> </div>
At this point the captcha is empty and nothing is displayed.
Steps:
One, to the CAPTCHA picture control plus a connection, this connection is an. aspx Web page, this page does not need the foreground, only need to open the background to do a verification code image display can be
<asp:image id= "Image1" runat= "Server" imageurl= "yzm.aspx"/>
Second, in the yzm.aspx backstage to make a simple verification code
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using System.drawing;public partial class yzm:system.web.ui.page{protected void Page_Load ( Object sender, EventArgs e) {Random r = new Random (); Make "Bitmap" (Specify a rectangular area of length and width) Bitmap img = new Bitmap (60,30); Ready to make-//set canvas Graphics g = Graphics.fromimage (IMG); Output string all = "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string s = ""; for (int i = 1; I <= 4; i++) {s + = all. Substring (R.next (All). Length), 1); }//String fonts font f=new font ("Microsoft Jas Black", 16); The color of the font Brush b=new SolidBrush (color.red); Starting position PointF p=new PointF (3,3); Making-g.drawstring (S, F, B, p); To save (save to stream) img. Save (response.outputstream,system.drawing.imaging.imageformat.png); Response.End (); }}
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using System.drawing;public partial class yzm:system.web.ui.page{protected void Page_Load ( Object sender, EventArgs e) {Random r = new Random (); Make "Bitmap" (Specify a rectangular area of length and width) Bitmap img = new Bitmap (60,30); Ready to make-//set canvas Graphics g = Graphics.fromimage (IMG); Output string all = "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string s = ""; for (int i = 1; I <= 4; i++) {s + = all. Substring (R.next (All). Length), 1); }//String fonts font f=new font ("Microsoft Jas Black", 16); The color of the font Brush b=new SolidBrush (color.red); Starting position PointF p=new PointF (3,3); Making-g.drawstring (S, F, B, p); To save (save to stream) img. Save (response.outputstream,system.drawing.imaging.imageformat.png); Response.End (); }}
Effect:
Third, set < submit verification > function
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;public partial class _default:system.web.ui.page{ protected void Page_Load (object sender, EventArgs e) { btn_verification. Click+=btn_verification_click; } < Submit validation > button click event void Btn_verification_click (object sender, EventArgs e) { string s1 = Txt_ Useryzm.text; String s2 = session["YZM"]. ToString (); if (S1. ToUpper () = = S2. ToUpper ()) { Label1.Text = "Verified successfully! "; } else { Label1.Text = "Validation failed! "; } }}
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;public partial class _default:system.web.ui.page{ protected void Page_Load (object sender, EventArgs e) { btn_verification. Click+=btn_verification_click; } < Submit validation > button click event void Btn_verification_click (object sender, EventArgs e) { string s1 = Txt_ Useryzm.text; String s2 = session["YZM"]. ToString (); if (S1. ToUpper () = = S2. ToUpper ()) { Label1.Text = "Verified successfully! "; } else { Label1.Text = "Validation failed! "; } }}
Effect:
Verify that automatic refresh is successful
Four, set the Click Verification Code switch Verification code-front JS
<script type= "Text/javascript" > var a = 0; document.getElementById ("Image1"). onclick = function () { this.setattribute ("src", "yzm.aspx?id=" + a); a++; } </script>
<script type= "Text/javascript" > var a = 0; document.getElementById ("Image1"). onclick = function () { this.setattribute ("src", "yzm.aspx?id=" + a); a++; } </script>
Set the captcha background color and the interference line fills the rectangular area: FillRectangle
Sets the background color g.fillrectangle (New SolidBrush (Clist[r.next (clist. Count)]), 0, 0, (+); Set the interference line for (int i = 1; i <=; i++) { //random colors color c_line = Clist[r.next (0, CList. Count)]; Interference line color, thickness Pen p_line = new Pen (new SolidBrush (C_line), R.next (1, 5)); Draw the interference line G.drawline (P_line, New PointF (r.next (0, 0), R.next (0,)), New PointF (R.next (0,))
Sets the background color g.fillrectangle (New SolidBrush (Clist[r.next (clist. Count)]), 0, 0, (+); Set the interference line for (int i = 1; i <=; i++) { //random colors color c_line = Clist[r.next (0, CList. Count)]; Interference line color, thickness Pen p_line = new Pen (new SolidBrush (C_line), R.next (1, 5)); Draw the interference line G.drawline (P_line, New PointF (r.next (0, 0), R.next (0,)), New PointF (R.next (0,))
===========================================
Verification Code picture Background code:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using System.drawing;public partial class yzm:system.web.ui.page{protected void Page_Load ( Object sender, EventArgs e) {Random r = new Random (); Color Set list<color> clist = new list<color> (); CList. ADD (Color.yellow); CList. ADD (Color.pink); CList. ADD (Color.Blue); CList. ADD (Color.green); CList. ADD (Color.orange); CList. ADD (Color.Black); Make "Bitmap" (Specify a rectangular area of length and width) Bitmap img = new Bitmap (70, 30); Set Canvas Graphics g = Graphics.fromimage (IMG); Sets the background color g.fillrectangle (new SolidBrush (Clist[r.next (clist. Count)]), 0, 0, 70, 30); Set the interference line for (int i = 1; i <=; i++) {//random colors color c_line = Clist[r.next (0, CList . Count)]; Interference line color, thickness Pen p_line = new Pen (new SolidBrush (C_line), R.next (1, 5)); Draw interference lines G.drawline (P_line, New PointF (R.next (0, 30), R.next (0,)), New PointF (R.next (0,), R.next (0,)); }//output string all = "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string s = ""; for (int i = 1; I <= 4; i++) {s + = all. Substring (R.next (All). Length), 1); }//generated validation is placed into global variables session["YZM"] = s; Font of the string fonts f = new Font ("Microsoft Jas Black", 16); The color of the font Brush b = new SolidBrush (color.red); Starting position PointF p = new PointF (3, 3); Making-g.drawstring (S, F, B, p); To save (save to stream) img. Save (Response.outputstream, System.Drawing.Imaging.ImageFormat.Png); Response.End (); }}
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using System.drawing;public partial class yzm:system.web.ui.page{protected void Page_Load ( Object sender, EventArgs e) {Random r = new Random (); Color Set list<color> clist = new list<color> (); CList. ADD (Color.yellow); CList. ADD (Color.pink); CList. ADD (Color.Blue); CList. ADD (Color.green); CList. ADD (Color.orange); CList. ADD (Color.Black); Make "Bitmap" (Specify a rectangular area of length and width) Bitmap img = new Bitmap (70, 30); Set Canvas Graphics g = Graphics.fromimage (IMG); Sets the background color g.fillrectangle (new SolidBrush (Clist[r.next (clist. Count)]), 0, 0, 70, 30); Set the interference line for (int i = 1; i <=; i++) {//random colors color c_line = Clist[r.next (0, CList . Count)]; Interference line color, thickness Pen p_line = new Pen (new SolidBrush (C_line), R.next (1, 5)); Draw interference lines G.drawline (P_line, New PointF (R.next (0, 30), R.next (0,)), New PointF (R.next (0,), R.next (0,)); }//output string all = "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string s = ""; for (int i = 1; I <= 4; i++) {s + = all. Substring (R.next (All). Length), 1); }//generated validation is placed into global variables session["YZM"] = s; Font of the string fonts f = new Font ("Microsoft Jas Black", 16); The color of the font Brush b = new SolidBrush (color.red); Starting position PointF p = new PointF (3, 3); Making-g.drawstring (S, F, B, p); To save (save to stream) img. Save (Response.outputstream, System.Drawing.Imaging.ImageFormat.Png); Response.End (); }}
c#-webform-★ make picture Verification code ★