1 /// <summary>
2 /// 举行左上角坐标和X、Y距离
3 /// </summary>
4 public class Coordinate
5 {
6 private int x;
7 private int y;
8 private int xDistance = 0;
9 private int yDistance = 0;
10
11 /// <summary>
12 /// X距离
13 /// </summary>
14 public int XDistance
15 {
16 get { return xDistance; }
17 set { xDistance = value; }
18 }
19
20 /// <summary>
21 /// Y距离
22 /// </summary>
23 public int YDistance
24 {
25 get { return yDistance; }
26 set { yDistance = value; }
27 }
28
29 /// <summary>
30 /// X点坐标
31 /// </summary>
32 public int X
33 {
34 get { return x; }
35 set { x = value; }
36 }
37
38 /// <summary>
39 /// Y点坐标
40 /// </summary>
41 public int Y
42 {
43 get { return y; }
44 set { y = value; }
45 }
46 }
Code
1 /// <summary>
2 /// 反色矩形集合生产器
3 /// </summary>
4 public class ReverseCoordinates
5 {
6 /// <summary>
7 /// 单件模式 实例
8 /// </summary>
9 public static readonly ReverseCoordinates Instance = new ReverseCoordinates();
10 /// <summary>
11 /// 总长度
12 /// </summary>
13 public const int TotalLenght = 650;
14
15 /// <summary>
16 /// 总宽度
17 /// </summary>
18 public const int TotalHeight = 250;
19 private List<Coordinate> list = new List<Coordinate>();
20
21 private ReverseCoordinates()
22 { }
23
24
25 /// <summary>
26 /// 返回要反色矩形左上角坐标的集合
27 /// </summary>
28 /// <returns>反色矩形左上角坐标的集合</returns>
29 public List<Coordinate> GetList()
30 {
31 this.CreateList();
32
33 return this.list;
34 }
35
36 /// <summary>
37 /// 创建反色矩形左上角坐标的集合
38 /// </summary>
39 private void CreateList()
40 {
41 Random random = new Random(Guid.NewGuid().GetHashCode());
42
43 //创建4个角反色矩形的长和宽
44 int cornerXDistance = random.Next(1, Convert.ToInt32(TotalLenght / 15));
45 int cornerYDistance = random.Next(1, Convert.ToInt32(TotalLenght / 5));
46
47 this.list = new List<Coordinate>();
48
49 //创建左上角反色矩形
50 Coordinate coordinate = new Coordinate();
51
52 coordinate.X = 1;
53 coordinate.Y = 1;
54 coordinate.XDistance = cornerXDistance;
55 coordinate.YDistance = cornerYDistance;
56
57 this.list.Add(coordinate);
58
59 //创建左下角反色矩形
60 coordinate = new Coordinate();
61
62 coordinate.X = 1;
63 coordinate.Y = TotalHeight - cornerYDistance;
64 coordinate.XDistance = cornerXDistance;
65 coordinate.YDistance = cornerYDistance;
66
67 this.list.Add(coordinate);
68
69 //创建右上角反色矩形
70 coordinate = new Coordinate();
71
72 coordinate.X = TotalLenght - cornerXDistance;
73 coordinate.Y = 1;
74 coordinate.XDistance = cornerXDistance;
75 coordinate.YDistance = cornerYDistance;
76
77 this.list.Add(coordinate);
78
79 //创建右下角反色矩形
80 coordinate = new Coordinate();
81
82 coordinate.X = TotalLenght - cornerXDistance;
83 coordinate.Y = TotalHeight - cornerYDistance;
84 coordinate.XDistance = cornerXDistance;
85 coordinate.YDistance = cornerYDistance;
86
87 this.list.Add(coordinate);
88
89 //创建中间大的反色矩形
90 coordinate = new Coordinate();
91
92 //得到每个字的大概大小
93 int fontSize = Convert.ToInt32(TotalLenght / 3.3);
94
95 //得到每个字大概中间的部位的X值
96 int[] sizeArray = new int[4];
97
98 sizeArray[0] = fontSize;
99 sizeArray[1] = fontSize * 1 - fontSize;
100 sizeArray[2] = fontSize * 2 - fontSize;
101 sizeArray[3] = fontSize * 3 - fontSize;
102
103 //随机选取中间反色矩形的X值
104 int index = random.Next(0, 3);
105
106 //得到中间反色矩形和左上角矩形的距离
107 int beginX = sizeArray[index] - cornerXDistance;
108
109 //如果两个矩形重叠
110 if (beginX < 0)
111 {
112 //将负的距离变正数
113 beginX *= -1;
114 }
115 else
116 {
117 beginX = 0;
118 }
119
120 //为了每次中间的反色矩形位置都不一样计算偏移值
121 int offset = random.Next(beginX, Convert.ToInt32(fontSize / 2));
122
123 coordinate.X = sizeArray[index] + offset;
124 coordinate.Y = random.Next(1, Convert.ToInt32(TotalHeight / 2));
125 coordinate.XDistance = random.Next(Convert.ToInt32(TotalLenght / 6), Convert.ToInt32(TotalLenght / 3));
126 coordinate.YDistance = random.Next(Convert.ToInt32(TotalHeight / 2), TotalHeight - 1);
127
128 this.list.Add(coordinate);
129 }
130
131 }
Code
1 /// <summary>
2 /// 验证码生成器
3 /// </summary>
4 public class ValidateCode
5 {
6 private string checkCode;
7
8 /// <summary>
9 /// 验证码
10 /// </summary>
11 public string CheckCode
12 {
13 get { return checkCode; }
14 set { checkCode = value; }
15 }
16
17 public ValidateCode()
18 {
19 this.GenerateCheckCode();
20 }
21
22 /// <summary>
23 /// 创建随机字符串
24 /// </summary>
25 private void GenerateCheckCode()
26 {
27 int number;
28 char code;
29 this.checkCode = String.Empty;
30 Random random = new Random();
31 for (int i = 0; i < 4; i++)
32 {
33 number = random.Next();
34 code = (char)('0' + (char)(number % 10));
35 this.checkCode += code.ToString();
36 }
37 }
38
39 /// <summary>
40 /// 生产图片
41 /// </summary>
42 public void CreateCheckCodeImage()
43 {
44 ReverseCoordinates coordinates = ReverseCoordinates.Instance;
45
46 int totalLenght = ReverseCoordinates.TotalLenght;
47 int totalHeight = ReverseCoordinates.TotalHeight;
48
49 Bitmap image = null;
50 Graphics graphics = null;
51 MemoryStream ms = null;
52
53 try
54 {
55 image = new Bitmap(totalLenght + 1, totalHeight + 1);
56 graphics = Graphics.FromImage(image);
57 ms = new MemoryStream();
58
59 //清空图片背景色
60 graphics.Clear(Color.White);
61 Font fontA = new Font("Arial", Convert.ToInt32(totalLenght / 3.3), (FontStyle.Bold | FontStyle.Italic));
62
63 Random random = new Random(Guid.NewGuid().GetHashCode());
64 int offset = random.Next(-5, 0);
65
66 graphics.DrawString(checkCode, fontA, Brushes.Black, offset, offset);
67
68 Color pixel;
69
70 List<Coordinate> list = coordinates.GetList();
71
72 //逐点反色
73 foreach (Coordinate coordinate in list)
74 {
75 int x = coordinate.X;
76
77 for (int i = 0; i < coordinate.XDistance && x < totalLenght; i++, x++)
78 {
79 int y = coordinate.Y;
80
81 for (int j = 0; j < coordinate.YDistance && y < totalHeight; j++, y++)
82 {
83 int r, g, b;
84 pixel = image.GetPixel(x, y);
85
86 r = 255 - pixel.R;
87 g = 255 - pixel.G;
88 b = 255 - pixel.B;
89
90 image.SetPixel(x, y, Color.FromArgb(r, g, b));
91 }
92 }
93 }
94
95 //画图片的边框线
96 graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
97
98 image.Save(ms, ImageFormat.Gif);
99
100 HttpResponse httpResponse = HttpContext.Current.Response;
101 httpResponse.ClearContent();
102 httpResponse.Expires = 0;
103 httpResponse.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
104 httpResponse.ContentType = "image/Gif";
105 httpResponse.BinaryWrite(ms.ToArray());
106 }
107 finally
108 {
109 graphics.Dispose();
110 image.Dispose();
111 ms.Dispose();
112 }
113 }
114 }