Implementation code
<summary>///Generate Verification code picture, save session name Verificationcode///</summary> public static void Createverific Ationcode () {int number; String Checkcode = String. Empty; Random number of seeds random randoms = new Random (); for (int i = 0; i < 4; i++)//Check code length is 4 {//random integer number = randoms. Next (); Characters are randomly generated from 0-9,a-z, and the corresponding ASCII codes are//48-57,65-90 number = number% 36; if (number <) {number + = 48; } else {number + = 55; } Checkcode + = ((char) number). ToString (); }//Save check code in Session system.web.httpcontext.current.session["verificationcode"] = Checkcode; If the check code is empty, return directly if (Checkcode = = NULL | | Checkcode.trim () = = String.Empty) {return; }//Determine the length of the output image according to the length of the checksum System.Drawing.Bitmap image = new System.Drawing.Bitmap (55//(int) math.ceiling (convert.todouble (checkcode.length * 15))//Create Graphics object Graphics g = Graphics.from Image (image); try {//Generate random number of seeds random random = new random (); Empty the picture background color g.clear (color.white); Draw the background noise line of the picture 10 bar//---------------------------------------------------for (int i = 0; i <; i++ ) {//Noise line start coordinate (x1,y1), end coordinate (x2,y2) int x1 = random. Next (image. Width); int x2 = random. Next (image. Width); int y1 = random. Next (image. Height); int y2 = random. Next (image. Height); Draw the noise line with Silver G.drawline (new Pen (color.silver), x1, y1, x2, y2); }//---------------------------------------------------//brush B = brushes.silver; G.fillrectangle (b, 0, 0, image. Width, image. Height); ---------------------either of these two options------------------------------//Output picture Lieutenant Colonel Code font: No. 12th Arial, bold italic font font = new Font ("Arial", FontStyle.Bold | Fontstyle.italic)); Linear gradient brush LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height), Color.Blue, Color.purple, 1.2f, true); g.DrawString (Checkcode, Font, brush, 2, 2); Picture the foreground noise point 50 for (int i = 0; i <; i++) {int x = random. Next (image. Width); int y = random. Next (image. Height); Image. SetPixel (x, Y, Color.FromArgb (random. Next ())); }//Draw the border line of the picture G.drawrectangle (new Pen (Color.peru), 0, 0, image. Width-1, image. HEIGHT-1); Create memory stream for output picture using (MemoryStream ms = new MemoryStream ()) {//Picture format specified as PNG Image. Save (MS, Imageformat.jpeg); Clears all output from the buffer stream System.Web.HttpContext.Current.Response.ClearContENT (); The HTTP MIME type of the output stream is set to "image/png" System.Web.HttpContext.Current.Response.ContentType = "image/jpeg"; Output picture of binary stream System.Web.HttpContext.Current.Response.BinaryWrite (Ms. ToArray ()); }} finally {//releases the bitmap object and the Graphics object G.dispose (); Image. Dispose (); } }
Create an ASPX page
<%@ page language= "C #" autoeventwireup= "true" codefile= "AuthCode.aspx.cs" inherits= "Authcode"%> <% Help.createverificationcode (); %>
Add HTML code, reference
<div class= "Positionr" > <label> Verification Code:</label> <span class= "Style1" > *</span> <input type= "text" class= "YANZM" runat= "Server" reg= "^.+$" id= "Txtauthcode" tip= "Please enter the verification code!"/> </div>
How do I implement a refresh?
<script type= "Text/javascript" > $ ("#imgAuthCode"). Click (function () { $ (this). attr ("src", " Authcode.aspx?code= "+ (new Date ()). GetTime ()); }); </script>