Generation of C # verification code

Source: Internet
Author: User

Html

<ul>   <Li>Verification Code:</Li>   <Li>    <imgsrc= "/login/getvalidatecode?id=1"ID= "Imgcode"alt= "click Refresh"onclick= "Clickchangecode ()"  />    <ahref= "javascript:void (0)"onclick= "Clickchangecode (); return false;">Can't see clearly</a>   </Li></ul>

Js

<script type= "Text/javascript" >    function  clickchangecode () {        var code = $ ("#imgCode"). attr ("src");        $ ("#imgCode"). attr ("src", Code + "1");    } </script>

C#

 Public classvalidatecode{ Public intMaxLength {Get{return Ten; } }     Public intMinLength {Get{return 1; } }    //Generate verification Code     Public stringCreatevalidatecode (intlength) {        int[] Randmembers =New int[length]; int[] Validatenums =New int[length]; stringValidatenumberstr =""; //generate a starting sequence value        intSeekseek =unchecked((int) DateTime.Now.Ticks); Random Seekrand=NewRandom (Seekseek); intBeginseek = (int) Seekrand.next (0, int32.maxvalue-length*10000); int[] seeks =New int[length];  for(inti =0; i < length; i++) {Beginseek+=10000; Seeks[i]=Beginseek; }        //Generate random numbers         for(inti =0; i < length; i++) {Random rand=NewRandom (Seeks[i]); intPownum =1*(int) Math.pow (Ten, length); Randmembers[i]=Rand.        Next (Pownum, Int32.MaxValue); }        //extracting random numbers         for(inti =0; i < length; i++)        {            stringNumstr =Randmembers[i].            ToString (); intNumlength =numstr.length; Random Rand=NewRandom (); intNumposition = Rand. Next (0, Numlength-1); Validatenums[i]= Int32.Parse (numstr.substring (Numposition,1)); }        //Generate verification Code         for(inti =0; i < length; i++) {Validatenumberstr+=Validatenums[i].        ToString (); }        returnValidatenumberstr; }    //generate a picture of the captcha     Public byte[] Createvalidategraphic (stringValidatecode) {Bitmap image=NewBitmap ((int) Math.ceiling (validatecode.length*12.0), A); Graphics g=graphics.fromimage (image); Try        {            //Generate random GeneratorsRandom random =NewRandom (); //clear the background color of the pictureg.clear (Color.White); //interference lines for drawing pictures             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); } Font Font=NewFont ("Arial", A, (FontStyle.Bold |fontstyle.italic)); LinearGradientBrush Brush=NewLinearGradientBrush (NewRectangle (0,0, image. Width, image. Height),
Color.Blue, Color.darkred,1.2f,true); g.DrawString (Validatecode, font, brush,3,2); //foreground interference point of picture painting 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); //Save picture DataMemoryStream stream =NewMemoryStream (); Image. Save (stream, imageformat.jpeg); //Output Picture Stream returnStream. ToArray (); } finally{g.dispose (); Image. Dispose (); } } //get the length of the Verification code picture Public Static intGetimagewidth (intvalidatenumlength) { return(int) (validatenumlength*12.0); } //get the height of the verification code Public Static Doublegetimageheight () {return 22.5; }}

Call

 PublicActionResult Checkcode () {//class that instantiates the CAPTCHA firstValidatecode Validatecode =NewValidatecode (); //generates the length specified by the Verification code    stringCode = Validatecode.createvalidatecode (5); //Assigning a verification code to a session variablesession["Validatecode"] =Code; //Create a picture of the verification code    byte[] bytes =validatecode.createvalidategraphic (code); //Finally, the verification code is returned    returnFile (Bytes,@"Image/jpeg");}

Generation of C # verification code

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.