随机验证码

1 using System;
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Web;
7 using System.Web.SessionState;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.HtmlControls;
11
12 namespace tenfu_OA
13 {
14 /// <summary>
15 /// imgcode 的摘要说明。
16 /// </summary>
17 public partial class imgcode : System.Web.UI.Page
18 {
19 protected void Page_Load(object sender, System.EventArgs e)
20 {
21 // 在此处放置用户代码以初始化页面
22 string checkcode=GenerateCheckCode();
23 CreateCheckCodeImage(checkcode);
24 }
25
26 #region Web 窗体设计器生成的代码
27 override protected void OnInit(EventArgs e)
28 {
29 //
30 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
31 //
32 InitializeComponent();
33 base.OnInit(e);
34 }
35
36 /// <summary>
37 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
38 /// 此方法的内容。
39 /// </summary>
40 private void InitializeComponent()
41 {
42 }
43 #endregion
44
45 private string GenerateCheckCode()
46 { //产生五位的随机字符串
47 int number;
48 char code;
49 string checkCode = String.Empty;
50
51 System.Random random = new Random();
52
53 for (int i = 0; i < 5; i++)
54 {
55 number = random.Next();
56
57 //if (number % 2 == 0)
58 code = (char)('0' + (char)(number % 10));
59 //else
60 //code = (char)('A' + (char)(number % 26));
61
62 checkCode += code.ToString();
63 }
64
65 //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
66 Session["checkcode"] = checkCode;//用于客户端校验码比较
67
68 return checkCode;
69 }
70
71 private void CreateCheckCodeImage(string checkCode)
72 { //将验证码生成图片显示
73 if (checkCode == null || checkCode.Trim() == String.Empty)
74 return;
75 //System.Drawing.Bitmap image2 = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
76 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
77 Graphics g = Graphics.FromImage(image);
78 //g.DrawImage(image2,0,0);
79 try
80 {
81 //生成随机生成器
82 Random random = new Random();
83
84 //清空图片背景色
85 g.Clear(Color.White);
86
87 //画图片的背景噪音线
88 for (int i = 0; i < 25; i++)
89 {
90 int x1 = random.Next(image.Width);
91 int x2 = random.Next(image.Width);
92 int y1 = random.Next(image.Height);
93 int y2 = random.Next(image.Height);
94
95 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
96 }
97
98 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
99 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);
100 g.DrawString(checkCode, font, brush, 2, 2);
101
102 //画图片的前景噪音点
103 // for (int i = 0; i < 100; i++)
104 // {
105 // int x = random.Next(image.Width);
106 // int y = random.Next(image.Height);
107 //
108 // image.SetPixel(x, y, Color.FromArgb(random.Next()));
109 // }
110
111 //画图片的边框线
112 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
113
114 System.IO.MemoryStream ms = new System.IO.MemoryStream();
115 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
116 Response.ClearContent();
117 Response.ContentType = "image/Gif";
118 Response.BinaryWrite(ms.ToArray());
119 ms.Close();
120 }
121 finally
122 {
123 g.Dispose();
124 image.Dispose();
125 }
126 }
127 }
128 }
posted @ 2011-04-01 10:24  xfyn  阅读(273)  评论(0)    收藏  举报