1 /// <summary> 2 /// 验证码类 3 /// </summary> 4 public class ValidateCode 5 { 6 /// <summary> 7 /// 验证码的最大长度 8 /// </summary> 9 public int MaxLength { get { return 10; } } 10 11 /// <summary> 12 /// 验证码的最小长度 13 /// </summary> 14 public int MinLength { get { return 1; } } 15 16 /// <summary> 17 /// 生成指定长度的验证码字符串 18 /// </summary> 19 /// <param name="length"></param> 20 /// <returns></returns> 21 public string CreateValidateCodeStr(int length) 22 { 23 //生成起始序列值 24 int seed = unchecked((int)DateTime.Now.Ticks); 25 Random seedRandom = new Random(seed); 26 int beginSeed = seedRandom.Next(0, Int32.MaxValue - length * 10000); 27 int[] seeds=new int[length]; 28 for (int i = 0; i < length; i++) 29 { 30 beginSeed += 10000; 31 seeds[i] = beginSeed; 32 } 33 34 //生成随机数 35 int[] randMembers = new int[length]; 36 for (int i = 0; i < length; i++) 37 { 38 Random rand = new Random(seeds[i]); 39 int p = 1 * (int)Math.Pow(10, length); 40 randMembers[i] = rand.Next(p, Int32.MaxValue); 41 } 42 43 //抽取随机数字 44 int[] validateNums=new int[length]; 45 for (int i = 0; i < length; i++) 46 { 47 string numStr = randMembers[i].ToString(); 48 int numLength = numStr.Length; 49 Random rand = new Random(); 50 int numPosition = rand.Next(0, numLength - 1); 51 validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1)); 52 } 53 54 //生成验证码 55 string validateCodeStr = ""; 56 for (int i = 0; i < length; i++) 57 { 58 validateCodeStr += validateNums[i].ToString(); 59 } 60 61 return validateCodeStr; 62 } 63 64 /// <summary> 65 /// 创建验证码的图片 66 /// </summary> 67 /// <param name="validateCodeStr"></param> 68 /// <returns></returns> 69 public byte[] CreateValidateImg(string validateCodeStr) 70 { 71 Bitmap bitmap = new Bitmap((int)Math.Ceiling(validateCodeStr.Length * 12.0), 22); 72 Graphics g = Graphics.FromImage(bitmap); 73 try 74 { 75 //清空图片的背景色 76 g.Clear(Color.White); 77 //实例化随机生成器 78 Random rand = new Random(); 79 //画图片的干扰线 80 for (int i = 0; i < 25; i++) 81 { 82 int x1 = rand.Next(bitmap.Width); 83 int y1 = rand.Next(bitmap.Height); 84 int x2 = rand.Next(bitmap.Width); 85 int y2 = rand.Next(bitmap.Height); 86 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 87 } 88 //画字符串 89 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); 90 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, bitmap.Width, bitmap.Height), Color.Blue, Color.DarkRed, 1.2f, true); 91 g.DrawString(validateCodeStr, font, brush, 3, 2); 92 //画图片的前景干扰点 93 for (int i = 0; i < 100; i++) 94 { 95 int x = rand.Next(bitmap.Width); 96 int y = rand.Next(bitmap.Height); 97 bitmap.SetPixel(x, y, Color.FromArgb(rand.Next())); 98 } 99 //画图片的边框线 100 g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); 101 //保存图片数据 102 MemoryStream stream = new MemoryStream(); 103 bitmap.Save(stream, ImageFormat.Jpeg); 104 //输出图片流 105 return stream.ToArray(); 106 } 107 finally 108 { 109 g.Dispose(); 110 bitmap.Dispose(); 111 } 112 } 113 114 /// <summary> 115 /// 得到验证码图片的长度 116 /// </summary> 117 /// <param name="validateNumLength">验证码的长度</param> 118 /// <returns></returns> 119 public static int GetImgWidth(int validateNumLength) 120 { 121 return (int)(validateNumLength * 12.0); 122 } 123 124 /// <summary> 125 /// 得到验证码图片的高度 126 /// </summary> 127 /// <returns></returns> 128 public static double GetImgHeight() 129 { 130 return 22.5; 131 } 132 }
验证码类V1
时间: 2024-11-06 07:48:27