ASP. NET implementation of simple digital Verification code example

Source: Internet
Author: User

Call

CAPTCHA: <input type= "text" id= "txtvalidate" style= "Border:solid 1px #9B9B9B; width:85px;                        height:17px; "/>  


Background implementation
Rnd.aspx.cs

Using system;using system.collections;using system.configuration;using system.data;using System.Web;using System.web.security;using system.web.ui;using system.web.ui.htmlcontrols;using System.Web.UI.WebControls;using System.web.ui.webcontrols.webparts;using system.drawing;using System.drawing.imaging;public partial class Rnd: system.web.ui.page{private int codelen = 5;//randomly displays the number of characters private int fineness = 100;//picture clarity private int imgwidth =  65;//picture width private int imgheight = 20;//picture height private string fontFamily = "Roman";//font name private int fontSize = 12;    Font size private random random = new random ();        protected void Page_Load (object sender, EventArgs e) {string validatecode = Createvalidatecode ();        session["Randomnumber"] = Validatecode;        Bitmap Bitmap = new Bitmap (ImgWidth, imgheight);        Disturbbitmap (bitmap);        Drawvalidatecode (bitmap, Validatecode); Bitmap.    Save (Response.outputstream, imageformat.gif); } private String CreAtevalidatecode ()//Get random number {string validatecode = ""; for (int i = 0; i < Codelen; i++) {int n = random.        Next (10);//returns a less than maximum worth of random number validatecode + = n.tostring ();    } return Validatecode; The private void Disturbbitmap (Bitmap Bitmap)//Gets the background image {for (int i = 0; i < Bitmap. Width; i++) {for (int j = 0; J < bitmap. Height; J + +) {if (random. Next (<= this.fineness) {bitmap. SetPixel (i, J, color.white);//Gets the pixel color of the specified position}}}} private void Drawvalidatecode (Bit        Map Bitmap, String validatecode) {Graphics g = graphics.fromimage (bitmap);        Font font = new Font (fontFamily, fontSize, fontstyle.bold); g.DrawString (Validatecode, Font, Brushes.green, random.) Next ( -3, one), random. Next (-4, 1));//Draw text characters in the specified area}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.