protected void Page_Load(object sender, EventArgs e)
        {
            
// 在此处放置用户代码以初始化页面
            Encoding gb = Encoding.GetEncoding("gb2312");
            
object[] bytes = CreateRegionCode(4);//调用函数产生4个随机中文汉字编码  
            
//根据汉字编码的字节数组解码出中文汉字
            string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
            
string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
            
string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
            
string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
            Session[Config.Reg_ValidateChars] 
= str1 + str2 + str3 + str4;//用于与输入的验证码作对比
            CreateImage(str1 + str2 + str3 + str4);
        }
        
/// <summary>
        
/// 随机产生汉字
        
/// </summary>
        
/// <param name="strlength"></param>
        
/// <returns></returns>
        private static object[] CreateRegionCode(int strlength)
        {
            
string[] rBase = new String[16] { "0""1""2""3""4""5""6""7""8""9""a""b""c""d""e""f" };//定义一个字符串数组储存汉字编码的组成元素
            Random rnd = new Random();
            
object[] bytes = new object[strlength];//定义一个object数组用来
            for (int i = 0; i < strlength; i++)
            {
                
//区位码第1位
                int r1 = rnd.Next(1114);
                
string str_r1 = rBase[r1].Trim();
                
//区位码第2位
                rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值
                int r2;
                
if (r1 == 13)
                { r2 
= rnd.Next(07); }
                
else
                { r2 
= rnd.Next(016); }
                
string str_r2 = rBase[r2].Trim();
                
//区位码第3位
                rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
                
int r3 = rnd.Next(1016);
                
string str_r3 = rBase[r3].Trim();
                
//区位码第4位
                rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
                
int r4;
                
if (r3 == 10)
                { r4 
= rnd.Next(116); }
                
else if (r3 == 15)
                { r4 
= rnd.Next(015); }
                
else
                { r4 
= rnd.Next(016); }
                
string str_r4 = rBase[r4].Trim();
                
//定义两个字节变量存储产生的随机汉字区位码
                byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
                
byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
                
byte[] str_r = new byte[] { byte1, byte2 };//将两个字节变量存储在字节数组中
                bytes.SetValue(str_r, i);//将产生的一个汉字的字节数组放入object数组中
            }
            
return bytes;
        }
        
/// <summary>
        
/// 生成中文验证码图片
        
/// </summary>
        
/// <param name="checkCode"></param>
        private void CreateImage(string checkCode)
        {
            
if (checkCode == null || checkCode.Trim() == String.Empty)
                
return;
            System.Drawing.Bitmap image 
= new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 21.0)), 22);
            Graphics g 
= Graphics.FromImage(image);
            
try
            {
                Random random 
= new Random();//生成随机生成器
                g.Clear(Color.White);//清空图片背景色
                for (int i = 0; i < 20; i++)//画图片的背景噪音线
                {
                    
int x1 = random.Next(image.Width);
                    
int x2 = random.Next(image.Width);
                    
int y1 = random.Next(image.Height);
                    
int y2 = random.Next(image.Height);
                    g.DrawLine(
new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font 
= new System.Drawing.Font("楷体"12, (System.Drawing.FontStyle.Bold));
                System.Drawing.Drawing2D.LinearGradientBrush brush 
= new

            System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f,

            
true);
                g.DrawString(checkCode, font, brush, 
22);
                
for (int i = 0; i < 100; i++)//画图片的前景噪音点
                {
                    
int x = random.Next(image.Width);
                    
int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                g.DrawRectangle(
new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);//画图片的边框线
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.ContentType 
= "image/Gif";
                Response.BinaryWrite(ms.ToArray());
            }
            
finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
posted on 2011-09-15 08:31  大江  阅读(687)  评论(0编辑  收藏  举报