This article mainly introduces the complete code class of C # to generate beautiful verification code. Has a good reference value. Let's take a look at the little series.
If you don't say much, look at the code:
Using system;using system.web;using system.drawing;using system.security.cryptography;namespace DotNet.Utilities{// /<summary>///Verification code class///</summary> public class Rand {#region generate random numbers///<summary>//Generate Random numbers///</summary>//<param name= "Length" > Build length </param> public static string number (int lengt h) {return number (Length, false); }///<summary>//Generate random numbers///</summary>//<param name= "Length" > Build length </param>// <param name= "Sleep" > whether to block the current thread before the build to avoid duplicate </param> public static string number (int Length, bool Sleep) { if (Sleep) System.Threading.Thread.Sleep (3); string result = ""; System.Random random = new Random (); for (int i = 0; i < Length; i++) {result + = random. Next (10). ToString (); } return result; #endregion #region generate random letters and numbers///<summary>//Generate random letters and numbers////</summary>//<param name= "Intstr" > Generate length </param> public static string str (int length) {return Str (Length, false); }///<summary>//Generate random letters and numbers////</summary>//<param name= "Length" > Build length </param> <param name= "Sleep" > whether to block the current thread before the build to avoid duplicate </param> public static string Str (int Length, bool Sleep) { if (Sleep) System.Threading.Thread.Sleep (3); char[] Pattern = new char[] {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 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 result = ""; int n = pattern.length; System.Random random = new Random (~unchecked ((int) DateTime.Now.Ticks)); for (int i = 0; i < Length; i++) {int rnd = random. Next (0, N); Result + = Pattern[rnd]; } return result; } #endregion #region generate random pure letter random Numbers///<summary>//Generate random pure Letter random Numbers///</summary>//<param name= "intstr" > Generate length </param> public static string Str_char (int length) {R Eturn Str_char (Length, false); }///<summary>//Generate random pure Letter random Numbers///</summary>//<param name= "Length" > Build length </param> <param name= "Sleep" > whether to block the current thread before the build to avoid duplicates </param> public static string Str_char (int Length, BOOL Sleep) {if (Sleep) System.Threading.Thread.Sleep (3); char[] Pattern = new char[] {' 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 result = ""; int n = pattern.length; System.Random random = new Random (~unchecked ((int) DateTime.Now.Ticks)); for (int i = 0; i < Length; i++) {int rnd = random. Next (0, N); Result + = Pattern[rnd]; } return result; } #endregion}///<summary>//Verify Picture class///</summary> public class Yzmhelper { #region private field, private string text; Private Bitmap image; private int lettercount = 4; Verify number of code bits private int letterwidth = 16; The width range of a single font private int letterheight = 20; The height range of a single font private static byte[] randb = new Byte[4]; private static RNGCryptoServiceProvider Rand = new RNGCryptoServiceProvider (); Private font[] fonts = {New Font (new FontFamily ("Times New Roman"), +next (1), System.Drawing.FontStyle.Regular), New Font (New FontFamily ("Georgia"), + Next (1), System.Drawing.FontStyle.Regular), new Font (new FontFamily ("Arial"), + Next (1), System.Drawing.FontStyle.Regular), New Font (New FontFamily ("Comic Sans MS"), + next (1), SYSTEM.DRAWING.F Ontstyle.regular)}; #endregion #region Public Properties///<summary>/////</summary> return this.text; }}///<summary>///Verification code Picture///</summary> public Bitmap image {get {return This.imag E }} #endregIon #region Constructor Public Yzmhelper () {HttpContext.Current.Response.Expires = 0; HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddSeconds (-1); HttpContext.Current.Response.AddHeader ("Pragma", "no-cache"); HttpContext.Current.Response.CacheControl = "No-cache"; This.text = Rand.number (4); CreateImage (); } #endregion #region Private method///<summary>////To get the next random number///</summary>//<param name= "Max" > Maximum value </param> private static int Next (int max) {rand. GetBytes (RANDB); int value = Bitconverter.toint32 (randb, 0); Value = value% (max + 1); if (value < 0) value =-value; return value; }///<summary>///Get the next random number///</summary>//<param name= "min" > Min </param>/// Lt;param name= "Max" > Max value </param> private static int Next (int min, int max) {int value = Next (max-min) + min; return value; } #endregion #region Public method///<summary>////Draw Verification Code///</summary> publicly void CreateImage () {int int_imagewidth = this.text.Length * letterwidth; Bitmap image = New Bitmap (int_imagewidth, letterheight); Graphics g = graphics.fromimage (image); G.clear (Color.White); for (int i = 0; i < 2; i++) {int x1 = Next (image). WIDTH-1); int x2 = Next (image. WIDTH-1); int y1 = Next (image. HEIGHT-1); int y2 = Next (image. HEIGHT-1); G.drawline (New Pen (color.silver), x1, y1, x2, y2); } int _x = -12, _y = 0; for (int int_index = 0; Int_index < this.text.Length; int_index++) {_x + = Next (12, 16); _y = Next (-2, 2); String Str_char = This.text.Substring (Int_index, 1); Str_char = Next (1) = = 1? Str_char. ToLower (): Str_char. ToUpper (); Brush Newbrush = new SolidBrush (Getrandomcolor ()); Point thepos = new Point (_x, _y); g.DrawString (Str_char, Fonts[next (fonts. LENGTH-1)], Newbrush, Thepos); } for (int i = 0; i < i++) {int x = Next (image). WIDTH-1); int y = Next (image. HEIGHT-1); Image. SetPixel (x, Y, Color.FromArgb (next (0, 255), next (0, 255), next (0, 255)); } image = Twistimage (image, True, Next (1, 3), Next (4, 6)); G.drawrectangle (New Pen (Color.lightgray, 1), 0, 0, int_imagewidth-1, (letterHeight-1)); This.image = image; }///<summary>///font random color////</summary> public Color Getrandomcolor () {Random randomnum _first = new Random ((int) DateTime.Now.Ticks); System.Threading.Thread.Sleep (Randomnum_first.next (50)); Random randomnum_sencond = new Random ((int) DateTime.Now.Ticks); int int_red = Randomnum_first.next (180); int int_green = Randomnum_sencond.next (180); int int_blue = (int_red + int_green > 300)? 0:400-int_red-int_green; Int_blue = (INT_BLUE > 255)? 255:int_blue; Return Color.FromArgb (int_red, Int_green, Int_blue); }///<summary>//sinusoidal wave distorted picture///</summary>//<param name= "srcbmp" > Picture path </param> <param name= "Bxdir" > If the Twist is selected as true</param>//<param name= "nmultvalue" > Amplitude multiples of the waveform, the greater the degree of distortion, the more generally 3& lt;/param>//<param name= "Dphase" > Waveform starting phase, value range [0-2*PI] </param> public System.Drawing.Bitmap Twistim Age (Bitmap srcbmp, bool Bxdir, double dmultvalue, double dphase) {double PI = 6.283185307179586476925286766559; Bitmap destbmp = new Bitmap (srcbmp.width, srcbmp.height); Graphics graph = graphics.fromimage (destbmp); Graph. FillRectangle (New SolidBrush (Color.White), 0, 0, destbmp.width, destbmp.height); Graph. Dispose (); Double Dbaseaxislen = Bxdir? (double) Destbmp.height: (double) destbmp.width; for (int i = 0, i < destbmp.width; i++) {for (int j = 0; J < Destbmp.height; J + +) {Double dx = 0; DX = Bxdir? (PI * (double) j)/Dbaseaxislen: (PI * (double) i)/Dbaseaxislen; DX + = Dphase; Double dy = math.sin (dx); int noldx = 0, Noldy = 0; NOLDX = Bxdir? i + (int) (DY * dmultvalue): i; Noldy = Bxdir? J:j + (int) (DY * dmultvalue); Color color = Srcbmp.getpixel (i, j); if (noldx >= 0 && noldx < destbmp.width && noldy >= 0 && Noldy < Destbmp.hei ght) {destbmp.setpixel (NOLDX, noldy, color); }}} srcbmp.dispose (); return destbmp; } #endregion}}