上面是生成驗證碼以及驗證碼圖片 下麵是怎麼使用: ...
1 public class ValidateCode 2 { 3 public ValidateCode() 4 { 5 } 6 /// <summary> 7 /// 驗證碼的最大長度 8 /// </summary> 9 public int MaxLength 10 { 11 get { return 10; } 12 } 13 /// <summary> 14 /// 驗證碼的最小長度 15 /// </summary> 16 public int MinLength 17 { 18 get { return 1; } 19 } 20 /// <summary> 21 /// 生成驗證碼 22 /// </summary> 23 /// <param name="length">指定驗證碼的長度</param> 24 /// <returns>驗證碼字元串</returns> 25 public string CreateValidateCode(int length) 26 { 27 int[] randMembers = new int[length]; 28 int[] validateNums = new int[length]; 29 string validateNumberStr = ""; 30 //生成起始序列值 31 int seekSeek = unchecked((int)DateTime.Now.Ticks); 32 Random seekRand = new Random(seekSeek); 33 int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000); 34 int[] seeks = new int[length]; 35 for (int i = 0; i < length; i++) 36 { 37 beginSeek += 10000; 38 seeks[i] = beginSeek; 39 } 40 //生成隨機數字 41 for (int i = 0; i < length; i++) 42 { 43 Random rand = new Random(seeks[i]); 44 int pownum = 1 * (int)Math.Pow(10, length); 45 randMembers[i] = rand.Next(pownum, Int32.MaxValue); 46 } 47 //抽取隨機數字 48 for (int i = 0; i < length; i++) 49 { 50 string numStr = randMembers[i].ToString(); 51 int numLength = numStr.Length; 52 Random rand = new Random(); 53 int numPosition = rand.Next(0, numLength - 1); 54 validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1)); 55 } 56 //生成驗證碼 57 for (int i = 0; i < length; i++) 58 { 59 validateNumberStr += validateNums[i].ToString(); 60 } 61 return validateNumberStr; 62 } 63 //======================在webform中的寫法============================ 64 /// <summary> 65 /// 創建驗證碼的圖片 66 /// </summary> 67 /// <param name="containsPage">要輸出到的page對象</param> 68 /// <param name="validateNum">驗證碼</param> 69 public void CreateValidateGraphic(string validateCode, HttpContext context) 70 { 71 Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22); 72 Graphics g = Graphics.FromImage(image); 73 try 74 { 75 //生成隨機生成器 76 Random random = new Random(); 77 //清空圖片背景色 78 g.Clear(Color.White); 79 //畫圖片的干擾線 80 for (int i = 0; i < 25; i++) 81 { 82 int x1 = random.Next(image.Width); 83 int x2 = random.Next(image.Width); 84 int y1 = random.Next(image.Height); 85 int y2 = random.Next(image.Height); 86 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 87 } 88 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); 89 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), 90 Color.Blue, Color.DarkRed, 1.2f, true); 91 g.DrawString(validateCode, font, brush, 3, 2); 92 //畫圖片的前景干擾點 93 for (int i = 0; i < 100; i++) 94 { 95 int x = random.Next(image.Width); 96 int y = random.Next(image.Height); 97 image.SetPixel(x, y, Color.FromArgb(random.Next())); 98 } 99 //畫圖片的邊框線 100 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 101 //保存圖片數據 102 MemoryStream stream = new MemoryStream(); 103 image.Save(stream, ImageFormat.Jpeg); 104 //輸出圖片流 105 context.Response.Clear(); 106 context.Response.ContentType = "image/jpeg"; 107 context.Response.BinaryWrite(stream.ToArray()); 108 } 109 finally 110 { 111 g.Dispose(); 112 image.Dispose(); 113 } 114 } 115 /// <summary> 116 /// 得到驗證碼圖片的長度 117 /// </summary> 118 /// <param name="validateNumLength">驗證碼的長度</param> 119 /// <returns></returns> 120 public static int GetImageWidth(int validateNumLength) 121 { 122 return (int)(validateNumLength * 12.0); 123 } 124 /// <summary> 125 /// 得到驗證碼的高度 126 /// </summary> 127 /// <returns></returns> 128 public static double GetImageHeight() 129 { 130 return 22.5; 131 } 132 133 //===============在mvc中的寫法========================= 134 //C# MVC 升級版 135 /// <summary> 136 /// 創建驗證碼的圖片 137 /// </summary> 138 /// <param name="containsPage">要輸出到的page對象</param> 139 /// <param name="validateNum">驗證碼</param> 140 public byte[] CreateValidateGraphic(string validateCode) 141 { 142 Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22); 143 Graphics g = Graphics.FromImage(image); 144 try 145 { 146 //生成隨機生成器 147 Random random = new Random(); 148 //清空圖片背景色 149 g.Clear(Color.White); 150 //畫圖片的干擾線 151 for (int i = 0; i < 25; i++) 152 { 153 int x1 = random.Next(image.Width); 154 int x2 = random.Next(image.Width); 155 int y1 = random.Next(image.Height); 156 int y2 = random.Next(image.Height); 157 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 158 } 159 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); 160 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), 161 Color.Blue, Color.DarkRed, 1.2f, true); 162 g.DrawString(validateCode, font, brush, 3, 2); 163 //畫圖片的前景干擾點 164 for (int i = 0; i < 100; i++) 165 { 166 int x = random.Next(image.Width); 167 int y = random.Next(image.Height); 168 image.SetPixel(x, y, Color.FromArgb(random.Next())); 169 } 170 //畫圖片的邊框線 171 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 172 //保存圖片數據 173 MemoryStream stream = new MemoryStream(); 174 image.Save(stream, ImageFormat.Jpeg); 175 //輸出圖片流 176 return stream.ToArray(); 177 } 178 finally 179 { 180 g.Dispose(); 181 image.Dispose(); 182 } 183 } 184 }
上面是生成驗證碼以及驗證碼圖片
下麵是怎麼使用:
1 string code=vCode.CreateValidateCode(4);//得到生成的驗證碼 2 byte[] bcode=vCode.CreateValidateGraphic(code);//將驗證碼創建成圖片的二進位格式