1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Drawing;4 usingSystem.Drawing.Imaging;5 usingSystem.IO;6 usingSystem.Linq;7 usingsystem.web;8 9 namespaceCascallTen { One Public classService_code:System.Web.UI.Page A { - #region - Public stringRandcode () the { - intCodecount =4;//generate 4 Random numbers - stringRandomchar ="2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z"; -Randomchar = Randomchar.replace (",",""); + stringRandomcode =""; -System.Threading.Thread.Sleep (3); + Char[] Allchararray =Randomchar.tochararray (); A intn =allchararray.length; atSystem.Random Random =NewRandom (~unchecked((int) (DateTime.Now.Ticks)) ; - for(inti =0; i < Codecount; i++) - { - intRnd = random. Next (0, n); -Randomcode + =Allchararray[rnd]; - } in returnRandomcode; - } to #endregion + #regionVerification Code - /// <summary> the ///Create a Captcha picture * /// </summary> $ /// <param name= "Randomcode" >Verification Code</param> Panax Notoginseng Public byte[] CreateImage (stringRandomcode) - { the intRandangle = +;//Random Rotation angle + intMapwidth = (int) (Randomcode. Length * -); ABitmap map =NewBitmap (Mapwidth, -);//Create a picture background theGraphics graph =graphics.fromimage (map); +Graph. Clear (Color.White);//Clear the screen, fill the background - //graph. DrawRectangle (New Pen (color.silver, 0), 0, 0, map. Width-1, map. HEIGHT-1);//Draw a border $ $Random Rand =NewRandom (); - - //verification code rotation to prevent machine identification the Char[] chars = Randomcode. ToCharArray ();//splitting strings into single-character arrays - //in text distanceWuyiStringFormat format =NewStringFormat (stringformatflags.noclip); theFormat. Alignment =Stringalignment.center; -Format. LineAlignment =Stringalignment.center; Wu //Define Color -Color[] C ={color.black, color.red, Color.Blue, Color.green, About Color.orange, Color.brown, color.darkblue}; $ - //draw a picture of the background noise line - for(inti =0; I <2; i++) - { A intx1 = Rand. Next (Ten); + intx2 = Rand. Next (map. Width-Ten, map. Width); the intY1 =Rand. Next (map. Height); - inty2 =Rand. Next (map. Height); $ theGraph. DrawLine (NewPen (C[rand. Next (7)]), x1, y1, x2, y2); the } the the for(inti =0; I < chars. Length; i++) - { in intCIndex = Rand. Next (7); the intFindex = Rand. Next (5); theFont f =NewSystem.Drawing.Font ("Arial", -, System.Drawing.FontStyle.Regular);//font Style (parameter 2 is font size) AboutBrush B =NewSystem.Drawing.SolidBrush (C[cindex]); thePoint dot =NewPoint ( A, -); the floatAngle = rand. Next (-randangle, Randangle);//Degrees of rotation theGraph. TranslateTransform (dot. X, Dot. Y);//move cursor to specified position + graph. RotateTransform (angle); -Graph. DrawString (Chars[i]. ToString (), F, B,1,1, format); theGraph. RotateTransform (-angle);//Turn back .BayiGraph. TranslateTransform (2,-dot. Y);//move cursor to specified position the } the //Create a picture -System.IO.MemoryStream ms =NewSystem.IO.MemoryStream (); -MemoryStream stream =NewMemoryStream (); the map. Save (stream, imageformat.jpeg); the graph. Dispose (); the map. Dispose (); the returnStream. ToArray (); - the the } the #endregion 94 } the}
Front desk
1 <labelclass= "Panel-body">Verification Code:</label>2 <inputtype= "text"ID= "Yzm"name= "Yzm"class= "Form-control" /> </TD>3 <imgID= "Validcode"style= "Cursor:pointer;"src= "/signin/yanzhengma"alt= "Verification Code"onclick= "this.src= '/signin/yanzhengma?time= ' + new Date (). GetTime ();" />4 <ahref="#"ID= "Changes"Data-toggle= "Modal"Data-target= "#mymodel">I can't see clearly.</a>
Js
$ ("#Changes"). Click (function () { document.getElementById ("Validcode"). src = '/signin/yanzhengma?time= ' + new Date (). GetTime (); });
Background call
1 /// <summary> 2 ///Generate verification Code3 /// </summary> 4 /// <returns></returns> 5 Publicactionresult Yanzhengma ()6 {7Service_code sc =NewService_code ();8 stringVverificationcode =SC. Randcode ();9session["Vcode"] =vverificationcode.tolower ();Ten //Safe.sessionadd ("Vcode", Vverificationcode.tolower (), 2); //Save verification Code in session One SC. CreateImage (Vverificationcode); A returnFile (SC. CreateImage (Vverificationcode),@"Image/jpeg"); -}
C # Good general purpose verification code