Code
1 ///*************************************************
2 /// 函数:GenerateCheckCode:生成四位随机数
3 /// CreateCheckCodeImg:绘制验证码
4 /// 作者:王洪剑
5 /// 日期:2009-6-9
6 ///*************************************************
7 //生成随机数
8 private string GenerateCheckCode()
9 {
10 int intNum;
11 char chrCode;
12 string strCheckCode = String.Empty;
13 Random random = new Random();
14 for (int i = 0; i < 4; i++)
15 {
16 intNum = random.Next();
17 if (intNum % 2 == 0)
18 chrCode = (char)('1' + (char)(intNum % 10));
19 else
20 chrCode = (char)('A' + (char)(intNum % 26));
21 strCheckCode += chrCode.ToString();
22 }
23 Session["HopeCheckCode"] = strCheckCode.ToString();
24 return strCheckCode.ToString();
25 }
26
27 //绘制验证码
28 private void CreateCheckCodeImg(string strCheckCode)
29 {
30 if (strCheckCode == null || strCheckCode.Trim() == String.Empty)
31 return;
32 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((strCheckCode.Length * 10.5)), 20);
33 Graphics gr = Graphics.FromImage(image);
34 try
35 {
36 Random random = new Random();//生成随机生成器
37 gr.Clear(Color.Transparent);//清空背景色并绘制白色
38 //画图片的背景噪音线
39 for (int i = 0; i < 2; i++)
40 {
41 int x1 = random.Next(image.Width);
42 int x2 = random.Next(image.Width);
43 int y1 = random.Next(image.Height);
44 int y2 = random.Next(image.Height);
45 gr.DrawLine(new Pen(Color.Green), x1, x2, y1, y2);
46 Font font = new System.Drawing.Font("Times New Roman", 12, (System.Drawing.FontStyle.Bold));//字体
47 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);//笔刷渐变色
48 gr.DrawString(strCheckCode, font, brush, 2, 2);
49 }
50
51 //画图片前景噪音点
52 for (int i = 0; i < 30; i++)
53 {
54 int x = random.Next(image.Width);
55 int y = random.Next(image.Height);
56 image.SetPixel(x, y, Color.FromArgb(random.Next()));
57 }
58 //画边框线
59 gr.DrawRectangle(new Pen(Color.Transparent), 0, 0, image.Width - 1, image.Height - 1);
60 System.IO.MemoryStream ms = new System.IO.MemoryStream();
61 image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//生成图片格式
62 Response.ClearContent();
63 Response.ContentType = "image/Png";
64 Response.BinaryWrite(ms.ToArray());
65
66 }
67 catch (Exception ex)
68 { Response.Write("生成验证码失败:" + ex.Message); }
69 }
70
1 ///*************************************************
2 /// 函数:GenerateCheckCode:生成四位随机数
3 /// CreateCheckCodeImg:绘制验证码
4 /// 作者:王洪剑
5 /// 日期:2009-6-9
6 ///*************************************************
7 //生成随机数
8 private string GenerateCheckCode()
9 {
10 int intNum;
11 char chrCode;
12 string strCheckCode = String.Empty;
13 Random random = new Random();
14 for (int i = 0; i < 4; i++)
15 {
16 intNum = random.Next();
17 if (intNum % 2 == 0)
18 chrCode = (char)('1' + (char)(intNum % 10));
19 else
20 chrCode = (char)('A' + (char)(intNum % 26));
21 strCheckCode += chrCode.ToString();
22 }
23 Session["HopeCheckCode"] = strCheckCode.ToString();
24 return strCheckCode.ToString();
25 }
26
27 //绘制验证码
28 private void CreateCheckCodeImg(string strCheckCode)
29 {
30 if (strCheckCode == null || strCheckCode.Trim() == String.Empty)
31 return;
32 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((strCheckCode.Length * 10.5)), 20);
33 Graphics gr = Graphics.FromImage(image);
34 try
35 {
36 Random random = new Random();//生成随机生成器
37 gr.Clear(Color.Transparent);//清空背景色并绘制白色
38 //画图片的背景噪音线
39 for (int i = 0; i < 2; i++)
40 {
41 int x1 = random.Next(image.Width);
42 int x2 = random.Next(image.Width);
43 int y1 = random.Next(image.Height);
44 int y2 = random.Next(image.Height);
45 gr.DrawLine(new Pen(Color.Green), x1, x2, y1, y2);
46 Font font = new System.Drawing.Font("Times New Roman", 12, (System.Drawing.FontStyle.Bold));//字体
47 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);//笔刷渐变色
48 gr.DrawString(strCheckCode, font, brush, 2, 2);
49 }
50
51 //画图片前景噪音点
52 for (int i = 0; i < 30; i++)
53 {
54 int x = random.Next(image.Width);
55 int y = random.Next(image.Height);
56 image.SetPixel(x, y, Color.FromArgb(random.Next()));
57 }
58 //画边框线
59 gr.DrawRectangle(new Pen(Color.Transparent), 0, 0, image.Width - 1, image.Height - 1);
60 System.IO.MemoryStream ms = new System.IO.MemoryStream();
61 image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//生成图片格式
62 Response.ClearContent();
63 Response.ContentType = "image/Png";
64 Response.BinaryWrite(ms.ToArray());
65
66 }
67 catch (Exception ex)
68 { Response.Write("生成验证码失败:" + ex.Message); }
69 }
70