生成任意位数随机验证码

 

1.先写一个方法产生0—9,a-z 35个字符的不重复的随机数,并且位数作为参数传递

 

 /// <summary>
    
///产生指定位数的随机数 
    
/// </summary>
    
/// <param name="VcodeNum">产生几位数</param>
    
/// <returns></returns>
    private string RndNum(int VcodeNum)
    {
        
string Vchar = "0,1,2,3,4,5,6,7,8,9";
        Vchar 
= Vchar + ",A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
        Vchar 
= Vchar.ToLower();
        
string[] VcArray = Vchar.Split(',');
        
string VNum = "";//由于字符串很短,就不用StringBuilder了
        int temp = -1;//记录上次随机数值,尽量避免生产几个一样的随机数

        
//采用一个简单的算法以保证生成随机数的不同
        Random rand = new Random();
        
for (int i = 1; i < VcodeNum + 1; i++)
        {
            
if (temp != -1)
            {
                rand 
= new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
            }
            
int t = rand.Next(35);
            
if (temp != -1 && temp == t)
            {
                
return RndNum(VcodeNum);
            }
            temp 
= t;
            VNum 
+= VcArray[t];

        }
        
return VNum;
    }

 

2.然后定义一个方法 绘制验证码

 

 /// <summary>
    
/// 创建指定数组的验证码
    
/// </summary>
    
/// <param name="checkCode">验证码</param>
    private void CreateImage(string checkCode)
    {
        
int iwidth = (int)(checkCode.Length * 15);
        System.Drawing.Bitmap image 
= new System.Drawing.Bitmap(iwidth, 25);
        Graphics g 
= Graphics.FromImage(image);
        g.Clear(Color.White);
        
//定义颜色
        Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan, Color.Purple, Color.YellowGreen };
        
//定义字体            
        string[] font = { "Times New Roman""Verdana""Microsoft Sans Serif""Comic Sans MS""Arial""宋体" };
        Random rand 
= new Random();
        
//随机输出噪点
        for (int i = 0; i < 50; i++)
        {
            
int x = rand.Next(image.Width);
            
int y = rand.Next(image.Height);
            
//g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
            g.DrawLine(new Pen(Color.LightPink, 0), x, y, 5023);
        }

        
//输出不同字体和颜色的验证码字符
        for (int i = 0; i < checkCode.Length; i++)
        {
            
int cindex = rand.Next(7);//从7种颜色中随机取
            int findex = rand.Next(6);//从6种字体中随机取

            Font f 
= new System.Drawing.Font(font[findex], 13, System.Drawing.FontStyle.Regular);
            Brush b 
= new System.Drawing.SolidBrush(c[cindex]);
            
int ii = 4;
            
//偶数时生成的字符宽度为4,奇数时为2
            if ((i + 1% 2 == 0)
            {
                ii 
= 2;
            }
            g.DrawString(checkCode.Substring(i, 
1), f, b, 3 + (i * 12), ii);
        }
        
//画一个边框
        g.DrawRectangle(new Pen(Color.Black, 0), 00, image.Width - 1, image.Height - 1);

        
//输出到浏览器
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.ClearContent();
        Response.ContentType 
= "image/Jpeg";
        Response.BinaryWrite(ms.ToArray());
        g.Dispose();
        image.Dispose();
    }

3.在page_load事件中测试

 

 protected void Page_Load(object sender, EventArgs e)
    {

        
if (!Page.IsPostBack)
        {
            
string Code = this.RndNum(4);
            Session[
"Code"= Code;
            
this.CreateImage(Code);
        }
    }

 

posted @ 2011-06-03 20:11  流星剑  阅读(2406)  评论(0编辑  收藏  举报