Asp. NET generates random verification code

Source: Internet
Author: User

: (Flowing)

1. New folder for storing (bitmap) pictures in project

Graphic:

2, the front desk can add an ASP. NET control or any other to display the picture label, etc. (below)

    <div>        <asp:image id="Image1" runat="server " Imageurl=". /img/randomimg.gif" />    </div>

3. Generate random number (below)

        /// <summary>        ///Generate random numbers/// </summary>        /// <param name= "Codelength" ></param>        /// <returns></returns>         Public stringCreatecode (intcodelength) {            //I'm not doing this. Allow repetitive control, the reader needs to be able to do the appropriate control            stringso ="1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q, R,s,t,u,v,w,x,y,z"; string[] Strarr = so. Split (','); stringCode =""; Random Rand=NewRandom ();  for(inti =0; i < codelength; i++) {Code+ = Strarr[rand. Next (0, Strarr.length)]; }            returnCode; }

4, produce the verification picture (below)

        /// <summary>        ///generate a validation picture/// </summary>        /// <param name= "code" ></param>         Public voidCreateImage (stringcode) {            //set up Bitmap objects, (70,25) bitmap size controlBitmap image =NewBitmap ( -, -); //create a drawing layer based on the bitmap object created aboveGraphics g =graphics.fromimage (image); Webcolorconverter ww=NewWebcolorconverter (); G.clear ((Color) ww. ConvertFromString ("#FAE264"));//generate bitmap background colorRandom Random=NewRandom (); //draw a picture of the background noise line             for(inti =0; I < A; 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.green), x1, y1, x2, y2);//Color.lightgray Noise Line Color            }            //Create a Font objectFont font =NewFont ("Arial", the, FontStyle.Bold |fontstyle.italic); System.Drawing.Drawing2D.LinearGradientBrush Brush=NewSystem.Drawing.Drawing2D.LinearGradientBrush (NewRectangle (0,0, image. Width, image. Height), color.red, Color.gray,1.2f,true); g.DrawString (code, font, brush,0,0); //draw the border line of a pictureG.drawrectangle (NewPen (Color.green),0,0, image. Width-0, image. Height-0); //temporarily Save (bitmap) PicturesImage. Save (Server.MapPath (".. /IMG") +"\\RandomImg.gif", System.Drawing.Imaging.ImageFormat.Gif); }

5, call (see below) (Note: Here I just to do the test, the reader should also consider the local refresh problem, here I do not make an explanation)

        protected void Page_Load (object  sender, EventArgs e)        {            string checkcode = Createcode (4 ); // 4 means to generate a            random number session["checkcode"] = checkcode;            CreateImage (Checkcode);        }

  Welcome a better approach-WVQUSRTG

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.