C# 比较不错的通用验证码
1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Drawing.Imaging; 5 using System.IO; 6 using System.Linq; 7 using System.Web; 8 9 namespace CasCall 10 { 11 public class service_code : System.Web.UI.Page 12 { 13 #region 14 public string RandCode() 15 { 16 int CodeCount = 4;//生成4个随机数 17 string randomChar = "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"; 18 randomChar = randomChar.Replace(",", ""); 19 string RandomCode = ""; 20 System.Threading.Thread.Sleep(3); 21 char[] allCharArray = randomChar.ToCharArray(); 22 int n = allCharArray.Length; 23 System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); 24 for (int i = 0; i < CodeCount; i++) 25 { 26 int rnd = random.Next(0, n); 27 RandomCode += allCharArray[rnd]; 28 } 29 return RandomCode; 30 } 31 #endregion 32 #region 验证码 33 /// <summary> 34 /// 创建验证码图片 35 /// </summary> 36 /// <param name="randomcode">验证码</param> 37 public byte[] CreateImage(string randomcode) 38 { 39 int randAngle = 40; //随机转动角度 40 int mapwidth = (int)(randomcode.Length * 18); 41 Bitmap map = new Bitmap(mapwidth, 24);//创建图片背景 42 Graphics graph = Graphics.FromImage(map); 43 graph.Clear(Color.White);//清除画面,填充背景 44 //graph.DrawRectangle(new Pen(Color.Silver, 0), 0, 0, map.Width - 1, map.Height - 1);//画一个边框 45 46 Random rand = new Random(); 47 48 //验证码旋转,防止机器识别 49 char[] chars = randomcode.ToCharArray();//拆散字符串成单字符数组 50 //文字距中 51 StringFormat format = new StringFormat(StringFormatFlags.NoClip); 52 format.Alignment = StringAlignment.Center; 53 format.LineAlignment = StringAlignment.Center; 54 //定义颜色 55 Color[] c = { Color.Black, Color.Red, Color.Blue, Color.Green, 56 Color.Orange, Color.Brown, Color.DarkBlue }; 57 58 //画图片的背景噪音线 59 for (int i = 0; i < 2; i++) 60 { 61 int x1 = rand.Next(10); 62 int x2 = rand.Next(map.Width - 10, map.Width); 63 int y1 = rand.Next(map.Height); 64 int y2 = rand.Next(map.Height); 65 66 graph.DrawLine(new Pen(c[rand.Next(7)]), x1, y1, x2, y2); 67 } 68 69 for (int i = 0; i < chars.Length; i++) 70 { 71 int cindex = rand.Next(7); 72 int findex = rand.Next(5); 73 Font f = new System.Drawing.Font("Arial", 18, System.Drawing.FontStyle.Regular);//字体样式(参数2为字体大小) 74 Brush b = new System.Drawing.SolidBrush(c[cindex]); 75 Point dot = new Point(12, 16); 76 float angle = rand.Next(-randAngle, randAngle);//转动的度数 77 graph.TranslateTransform(dot.X, dot.Y);//移动光标到指定位置 78 graph.RotateTransform(angle); 79 graph.DrawString(chars[i].ToString(), f, b, 1, 1, format); 80 graph.RotateTransform(-angle);//转回去 81 graph.TranslateTransform(2, -dot.Y);//移动光标到指定位置 82 } 83 //生成图片 84 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 85 MemoryStream stream = new MemoryStream(); 86 map.Save(stream, ImageFormat.Jpeg); 87 graph.Dispose(); 88 map.Dispose(); 89 return stream.ToArray(); 90 91 92 } 93 #endregion 94 } 95 }
前台
1 <label class="panel-body">验证码:</label> 2 <input type="text" id="yzm" name="yzm" class="form-control" /> </td> 3 <img id="ValidCode" style="cursor: pointer;" src="/SignIn/YanZhengMa" alt="验证码" onclick="this.src='/SignIn/YanZhengMa?time=' + new Date().getTime();" /> 4 <a href="#" id="Changes" data-toggle="modal" data-target="#mymodel">看不清,换一张</a>
JS
$("#Changes").click(function () { document.getElementById("ValidCode").src = '/SignIn/YanZhengMa?time=' + new Date().getTime(); });
后台调用
1 /// <summary> 2 /// 生成验证码 3 /// </summary> 4 /// <returns></returns> 5 public ActionResult YanZhengMa() 6 { 7 service_code sc = new service_code(); 8 string vVerificationCode = sc.RandCode(); 9 Session["vcode"] = vVerificationCode.ToLower(); 10 // Safe.SessionAdd("vcode", vVerificationCode.ToLower(), 2); //Session中保存验证码 11 sc.CreateImage(vVerificationCode); 12 return File(sc.CreateImage(vVerificationCode), @"image/jpeg"); 13 }
效果图