本来想偷懒,在网络找验证码类,可是没找到自己需要的;有点伤心,看了一下别人写的,然后开始花了一个多小时解决问题, 放在这里 给我和不懂的盆友们查看备用 主要就以代码解释为主了,楼主语文重来不及格, 看code
1 using System;
2 using System.Drawing;
3 using System.Drawing.Drawing2D;
4 using System.Drawing.Imaging;
5 using System.IO;
6
7 namespace Tools
8 {
9 public class CheakCode
10 {
11 #region 构造
12
13 /// <summary>
14 /// 验证码类,随机生成4到5个字符
15 /// <example>
16 ///CheakCode cheakcode = new CheakCode();
17 ///
18 ///Response.ClearContent();
19 ///
20 ///Response.ContentType = "image/Gif";
21 ///
22 ///Response.BinaryWrite(cheakcode.GetImgWithValidateCode().ToArray());
23 ///
24 ///cheakcode = null;
25 /// </example>
26 /// </summary>
27 public CheakCode() { }
28
29 public CheakCode(string codeStr, int h, int w, int codeNum, int fSize)
30 {
31 this.codeStr = codeStr;
32 this.height = h;
33 this.width = w;
34 this.codeNum = codeNum;
35 this.fontSize = fSize;
36 }
37
38 public CheakCode(string codeStr)
39 {
40 this.codeStr = codeStr;
41 }
42
43 public CheakCode(int fSize, string codeStr)
44 {
45 this.codeStr = codeStr;
46 this.fontSize = fSize;
47 }
48
49 public CheakCode(string codeStr, int codeNum)
50 {
51 this.codeNum = codeNum;
52 this.codeStr = codeStr;
53 }
54
55 public CheakCode(int w, int h)
56 {
57 this.height = h;
58 this.width = w;
59 }
60
61 #endregion 构造
62
63 #region 属性
64
65 private int _width = 100;
66
67 /// <summary>
68 /// 验证码彩条宽度
69 /// </summary>
70 public int width { get { return this._width; } set { this._width = value; } }
71
72 private int _height = 40;
73
74 /// <summary>
75 /// 验证码彩条高度
76 /// </summary>
77 public int height { get { return this._height; } set { this._height = value; } }
78
79 private int _codeNum = new Random().Next(4, 6);
80
81 /// <summary>
82 /// 验证码彩条需要产生的个数
83 /// </summary>
84 public int codeNum { get { return this._codeNum; } set { this._codeNum = value; } }
85
86 private string _codeStr = Guid.NewGuid().ToString().Replace("-", "");// "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
87
88 /// <summary>
89 /// 验证码字符串 默认就拿Guid 产生字符
90 /// </summary>
91 public string codeStr { get { return this._codeStr; } set { this._codeStr = value; } }
92
93 private int _fontSize = 15;
94
95 /// <summary>
96 /// 验证码彩条字体大小
97 /// </summary>
98 public int fontSize { get { return this._fontSize; } set { this._fontSize = value; } }
99
100 /// <summary>
101 /// 彩条字体,我这里定义了一些,使用中随机抽取一种
102 /// </summary>
103 private String[] fontFamily ={ "Arial", "Verdana", "Comic Sans MS", "Impact", "Haettenschweiler",
104 "Lucida Sans Unicode", "Garamond", "Courier New", "Book Antiqua", "Arial Narrow" };
105
106 /// <summary>
107 /// 产生验证码字符串
108 /// </summary>
109 public string GetValidateCode
110 {
111 get
112 {
113 Random rd = new Random(); //创建随机数对象
114
115 string tempStr = null;
116 for (int i = 0; i < codeNum; i++)
117 {
118 tempStr = tempStr + this.codeStr[rd.Next(this._codeStr.Length - 1)];
119 }
120 return tempStr;
121 }
122 private set { }
123 }
124
125 #endregion 属性
126
127 /// <summary>
128 /// 随机字符串,随机颜色背景,和随机线条产生的Image
129 /// 返回 MemoryStream
130 /// </summary>
131 /// <returns>MemoryStream</returns>
132 public MemoryStream GetImgWithValidateCode()
133 {
134 //声明位图
135 Bitmap bitMap = null;
136
137 ///画笔
138 Graphics gph = null;
139 using (MemoryStream memStream = new MemoryStream())
140 {
141 Random random = new Random();
142 int fontWidth = (int)Math.Round(this.width / (this.codeNum + 2) / 1.0); //这里主要是适应字符的字符,让其能全部显示出来
143 int fontHeight = (int)Math.Round(this.height / 1.0);
144 this.fontSize = fontWidth <= fontHeight ? fontWidth : fontHeight; //取字体大小中间值
145 using (bitMap = new Bitmap(this.width, this.height))
146 {
147 gph = Graphics.FromImage(bitMap);
148 gph.Clear(GetControllableColor(200));
149 PointF Cpoint1 = new PointF(5, 5);
150 int x1 = 0, y1 = 0;
151 ///通过循环控制每一个字符
152 for (int i = 0; i < this.GetValidateCode.Length; i++)
153 {
154 //随机字符位置
155 x1 = random.Next(10) + 15 * i;
156 y1 = random.Next(bitMap.Height / 4);
157 Cpoint1 = new PointF(x1, y1);
158 SolidBrush solidBrush = new SolidBrush(Color.FromArgb(random.Next(100), random.Next(100), random.Next(100))); //调整颜色,使其文字能看见
159 Font textFont = new Font(this.fontFamily[random.Next(this.fontFamily.Length - 1)], this.fontSize, FontStyle.Bold);
160 gph.TranslateTransform(10, 0);
161 Matrix transform = gph.Transform;
162 if (i % 2 == 0) //这里我做了大小写处理 true 大写了
163 {
164 gph.DrawString(this.GetValidateCode.Substring(i, 1).ToUpper(), textFont, solidBrush, Cpoint1); //写字符
165 }
166 else
167 {
168 gph.DrawString(this.GetValidateCode.Substring(i, 1), textFont, solidBrush, Cpoint1); //写字符
169 }
170
171 gph.ResetTransform(); //重置矩阵
172 }
173 gph.DrawRectangle(new Pen(Color.Black, 2), 0, 0, bitMap.Width - 1, bitMap.Height - 1);// //画图片的边框线
174 try
175 {
176 bitMap.Save(memStream, ImageFormat.Gif);//保存内存流
177 }
178 catch (Exception ex)
179 {
180 throw ex;
181 }
182 finally
183 {
184 bitMap.Dispose();
185 random = null;
186 gph.Dispose();
187 }
188 return memStream;
189 }
190 }
191 }
192
193 /// <summary>
194 /// 产生随机颜色(R,G,B)
195 /// </summary>
196 /// <param name="colorBase"></param>
197 /// <returns>背景色</returns>
198 private Color GetControllableColor(int colorBase)
199 {
200 Color color = Color.Black;
201 if (colorBase > 200)
202 {
203 return color;
204 }
205 Random random = new Random();
206 color = Color.FromArgb(random.Next(56) + colorBase, random.Next(56) + colorBase, random.Next(56) + colorBase);
207 random = null;
208 return color;
209 }
210
211 /// <summary>
212 /// 判断验证码是否正确
213 /// </summary>
214 /// <param name="inputValCode">待判断的验证码</param>
215 /// <returns>正确返回 true,错误返回 false</returns>
216 public bool IsRight(string inputValCode)
217 {
218 if (this.GetValidateCode.ToUpper().Equals(inputValCode.ToUpper()))//无论输入大小写都转换为大些判断
219 {
220 return true;
221 }
222 else
223 {
224 return false;
225 }
226 }
227 }
228 }