首页 > 代码库 > MVC验证码生成类库
MVC验证码生成类库
1 public class ValidateCode 2 { 3 /// <summary> 4 /// 验证码的最大长度 5 /// </summary> 6 public int MaxLength 7 { 8 get { return 10; } 9 } 10 11 /// <summary> 12 /// 验证码的最小长度 13 /// </summary> 14 public int MinLength 15 { 16 get { return 1; } 17 } 18 19 /// <summary> 20 /// 生成验证码 21 /// </summary> 22 /// <param name="length">指定验证码的长度</param> 23 /// <returns></returns> 24 public string CreateValidateCode(int length) 25 { 26 var randMembers = new int[length]; 27 var validateNums = new int[length]; 28 string validateNumberStr = ""; 29 //生成起始序列值 30 var seekSeek = unchecked((int) DateTime.Now.Ticks); 31 var seekRand = new Random(seekSeek); 32 int beginSeek = seekRand.Next(0, Int32.MaxValue - length*10000); 33 var seeks = new int[length]; 34 for (int i = 0; i < length; i++) 35 { 36 beginSeek += 10000; 37 seeks[i] = beginSeek; 38 } 39 //生成随机数字 40 for (int i = 0; i < length; i++) 41 { 42 var rand = new Random(seeks[i]); 43 int pownum = 1*(int) Math.Pow(10, length); 44 randMembers[i] = rand.Next(pownum, Int32.MaxValue); 45 } 46 //抽取随机数字 47 for (int i = 0; i < length; i++) 48 { 49 string numStr = randMembers[i].ToString(); 50 int numLength = numStr.Length; 51 var rand = new Random(); 52 int numPosition = rand.Next(0, numLength - 1); 53 validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1)); 54 } 55 //生成验证码 56 for (int i = 0; i < length; i++) 57 { 58 validateNumberStr += validateNums[i].ToString(); 59 } 60 return validateNumberStr; 61 } 62 63 /// <summary> 64 /// 创建验证码的图片 65 /// </summary> 66 /// <param name="containsPage">要输出到的page对象</param> 67 /// <param name="validateNum">验证码</param> 68 public byte[] CreateValidateGraphic(string validateCode) 69 { 70 var image = new Bitmap((int) Math.Ceiling(validateCode.Length*20.0), 31); 71 Graphics g = Graphics.FromImage(image); 72 try 73 { 74 //生成随机生成器 75 var random = new Random(); 76 //清空图片背景色 77 g.Clear(Color.White); 78 //画图片的干扰线 79 for (int i = 0; i < 25; i++) 80 { 81 int x1 = random.Next(image.Width); 82 int x2 = random.Next(image.Width); 83 int y1 = random.Next(image.Height); 84 int y2 = random.Next(image.Height); 85 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 86 } 87 var font = new Font("Arial", 20, (FontStyle.Bold | FontStyle.Italic)); 88 var brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), 89 Color.Blue, Color.DarkRed, 1.2f, true); 90 g.DrawString(validateCode, font, brush, 3, 2); 91 //画图片的前景干扰点 92 for (int i = 0; i < 100; i++) 93 { 94 int x = random.Next(image.Width); 95 int y = random.Next(image.Height); 96 image.SetPixel(x, y, Color.FromArgb(random.Next())); 97 } 98 //画图片的边框线 99 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);100 //保存图片数据101 var stream = new MemoryStream();102 image.Save(stream, ImageFormat.Jpeg);103 //输出图片流104 return stream.ToArray();105 }106 finally107 {108 g.Dispose();109 image.Dispose();110 }111 }112 113 /// <summary>114 /// 得到验证码图片的长度115 /// </summary>116 /// <param name="validateNumLength">验证码的长度</param>117 /// <returns></returns>118 public static int GetImageWidth(int validateNumLength)119 {120 return (int) (validateNumLength*12.0);121 }122 123 /// <summary>124 /// 得到验证码的高度125 /// </summary>126 /// <returns></returns>127 public static double GetImageHeight()128 {129 return 22.5;130 }131 }
MVC验证码生成类库
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。