代码
1 using System;
2 using System.Drawing;
3 namespace cxyy.Web
4 {
5 public partial class ValidateCode : System.Web.UI.Page
6 {
7 private void Page_Load(object sender, System.EventArgs e)
8 {
9 string checkCode = GetRandomCode(4);
10 Session["CheckCode"] = checkCode;
11 SetPageNoCache();
12 CreateImage(checkCode);
13 }
14
15 /// <summary>
16 /// 设置页面不被缓存
17 /// </summary>
18 private void SetPageNoCache()
19 {
20 Response.Buffer = true;
21 Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
22 Response.Expires = 0;
23 Response.CacheControl = "no-cache";
24 Response.AppendHeader("Pragma", "No-Cache");
25 }
26
27 private string CreateRandomCode(int codeCount)
28 {
29 string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,M,N,P,Q,R,S,T,U,W,X,Y,Z";
30 string[] allCharArray = allChar.Split(',');
31 string randomCode = "";
32 int temp = -1;
33
34 Random rand = new Random();
35 for (int i = 0; i < codeCount; i++)
36 {
37 if (temp != -1)
38 {
39 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
40 }
41 int t = rand.Next(35);
42 if (temp == t)
43 {
44 return CreateRandomCode(codeCount);//性能问题
45 }
46 temp = t;
47 randomCode += allCharArray[t];
48 }
49 return randomCode;
50 }
51 private string GetRandomCode(int CodeCount)
52 {
53 string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,M,N,P,Q,R,S,T,U,W,X,Y,Z";
54 string[] allCharArray = allChar.Split(',');
55 string RandomCode = "";
56 int temp = -1;
57
58 Random rand = new Random();
59 for (int i = 0; i < CodeCount; i++)
60 {
61 if (temp != -1)
62 {
63 rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
64 }
65
66 int t = rand.Next(33);
67
68 while (temp == t)
69 {
70 t = rand.Next(33);
71 }
72
73 temp = t;
74 RandomCode += allCharArray[t];
75 }
76
77 return RandomCode;
78 }
79 private void CreateImage(string checkCode)
80 {
81 int iwidth = (int)(checkCode.Length * 14);
82 System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
83 Graphics g = Graphics.FromImage(image);
84 Font f = new System.Drawing.Font("Arial ", 10, System.Drawing.FontStyle.Bold);
85 Brush b = new System.Drawing.SolidBrush(Color.FromArgb(255,255,255));
86 Brush r = new System.Drawing.SolidBrush(Color.FromArgb(255,0,0));
87
88 //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
89 //g.Clear(Color.AliceBlue);//背景色
90 g.Clear(System.Drawing.ColorTranslator.FromHtml("#FD8BA6"));//背景色
91
92 char[] ch = checkCode.ToCharArray();
93 for (int i = 0; i < ch.Length; i++)
94 {
95 if (ch[i] >= '0' && ch[i] <= '9')
96 {
97 //数字用红色显示
98 g.DrawString(ch[i].ToString(), f, r, 3 + (i * 12), 3);
99 }
100 else
101 { //字母用黑色显示
102 g.DrawString(ch[i].ToString(), f, b, 3 + (i * 12), 3);
103 }
104 }
105
106 //for循环用来生成一些随机的水平线
107 // Pen blackPen = new Pen(Color.Black, 0);
108 // Random rand = new Random();
109 // for (int i=0;i<5;i++)
110 // {
111 // int y = rand.Next(image.Height);
112 // g.DrawLine(blackPen,0,y,image.Width,y);
113 // }
114
115 System.IO.MemoryStream ms = new System.IO.MemoryStream();
116 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
117 //history back 不重复
118 Response.Cache.SetNoStore();//这一句
119 Response.ClearContent();
120 Response.ContentType = "image/Jpeg";
121 Response.BinaryWrite(ms.ToArray());
122 g.Dispose();
123 image.Dispose();
124 }
125 }
126 }
127
2 using System.Drawing;
3 namespace cxyy.Web
4 {
5 public partial class ValidateCode : System.Web.UI.Page
6 {
7 private void Page_Load(object sender, System.EventArgs e)
8 {
9 string checkCode = GetRandomCode(4);
10 Session["CheckCode"] = checkCode;
11 SetPageNoCache();
12 CreateImage(checkCode);
13 }
14
15 /// <summary>
16 /// 设置页面不被缓存
17 /// </summary>
18 private void SetPageNoCache()
19 {
20 Response.Buffer = true;
21 Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
22 Response.Expires = 0;
23 Response.CacheControl = "no-cache";
24 Response.AppendHeader("Pragma", "No-Cache");
25 }
26
27 private string CreateRandomCode(int codeCount)
28 {
29 string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,M,N,P,Q,R,S,T,U,W,X,Y,Z";
30 string[] allCharArray = allChar.Split(',');
31 string randomCode = "";
32 int temp = -1;
33
34 Random rand = new Random();
35 for (int i = 0; i < codeCount; i++)
36 {
37 if (temp != -1)
38 {
39 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
40 }
41 int t = rand.Next(35);
42 if (temp == t)
43 {
44 return CreateRandomCode(codeCount);//性能问题
45 }
46 temp = t;
47 randomCode += allCharArray[t];
48 }
49 return randomCode;
50 }
51 private string GetRandomCode(int CodeCount)
52 {
53 string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,M,N,P,Q,R,S,T,U,W,X,Y,Z";
54 string[] allCharArray = allChar.Split(',');
55 string RandomCode = "";
56 int temp = -1;
57
58 Random rand = new Random();
59 for (int i = 0; i < CodeCount; i++)
60 {
61 if (temp != -1)
62 {
63 rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
64 }
65
66 int t = rand.Next(33);
67
68 while (temp == t)
69 {
70 t = rand.Next(33);
71 }
72
73 temp = t;
74 RandomCode += allCharArray[t];
75 }
76
77 return RandomCode;
78 }
79 private void CreateImage(string checkCode)
80 {
81 int iwidth = (int)(checkCode.Length * 14);
82 System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
83 Graphics g = Graphics.FromImage(image);
84 Font f = new System.Drawing.Font("Arial ", 10, System.Drawing.FontStyle.Bold);
85 Brush b = new System.Drawing.SolidBrush(Color.FromArgb(255,255,255));
86 Brush r = new System.Drawing.SolidBrush(Color.FromArgb(255,0,0));
87
88 //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
89 //g.Clear(Color.AliceBlue);//背景色
90 g.Clear(System.Drawing.ColorTranslator.FromHtml("#FD8BA6"));//背景色
91
92 char[] ch = checkCode.ToCharArray();
93 for (int i = 0; i < ch.Length; i++)
94 {
95 if (ch[i] >= '0' && ch[i] <= '9')
96 {
97 //数字用红色显示
98 g.DrawString(ch[i].ToString(), f, r, 3 + (i * 12), 3);
99 }
100 else
101 { //字母用黑色显示
102 g.DrawString(ch[i].ToString(), f, b, 3 + (i * 12), 3);
103 }
104 }
105
106 //for循环用来生成一些随机的水平线
107 // Pen blackPen = new Pen(Color.Black, 0);
108 // Random rand = new Random();
109 // for (int i=0;i<5;i++)
110 // {
111 // int y = rand.Next(image.Height);
112 // g.DrawLine(blackPen,0,y,image.Width,y);
113 // }
114
115 System.IO.MemoryStream ms = new System.IO.MemoryStream();
116 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
117 //history back 不重复
118 Response.Cache.SetNoStore();//这一句
119 Response.ClearContent();
120 Response.ContentType = "image/Jpeg";
121 Response.BinaryWrite(ms.ToArray());
122 g.Dispose();
123 image.Dispose();
124 }
125 }
126 }
127