`
lianxiangbus
  • 浏览: 528804 次
文章分类
社区版块
存档分类
最新评论

图片效果的验证码

 
阅读更多

下面贴个自己写的图片效果的验证码:代码比较简单.

 protected void Button1_Click(object sender, EventArgs e)
    {
        char[] num ={ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
        char[] letter ={ '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' };
        //循环四次从上面的二个表中提出4个对象,其中包含3个数字,1个字母
        string result="";
     
        for (int i=1; i <= 4; i++)
        {
            if (i <= 3)
            {
                    Random des = new Random(i*(int)DateTime.Now.Ticks);
                    result += num[des.Next(10)];
                 
            }
            else
            {
                Random des = new Random((int)DateTime.Now.Ticks);
                result += letter[des.Next(26)];
              }
              
        }
        //result包含3个数字和一个字母
        //现在随机将得到的结果排序
        Random sort = new Random();
        int identify = sort.Next(1, 4);//在1~4范围内获取排序因子
        string[] sortResult=new string[4];
        switch (identify) { 
            case 1:
                sortResult[0] = result.Substring(0, 1);
                sortResult[1] = result.Substring(1, 1);
                sortResult[2] = result.Substring(2, 1);
                sortResult[3] = result.Substring(3, 1);
                break;
            case 2:
                sortResult[0] = result.Substring(1, 1);
                sortResult[1] = result.Substring(2, 1);
                sortResult[2] = result.Substring(3, 1);
                sortResult[3] = result.Substring(0, 1);
                break;
            case 3:
                sortResult[0] = result.Substring(2, 1);
                sortResult[1] = result.Substring(3, 1);
                sortResult[2] = result.Substring(0, 1);
                sortResult[3] = result.Substring(1, 1);
                break;
            case 4:
                sortResult[0] = result.Substring(3, 1);
                sortResult[1] = result.Substring(0, 1);
                sortResult[2] = result.Substring(1, 1);
                sortResult[3] = result.Substring(2, 1);
                break;
        
        }
        //将随机化的数字整合成字符串
        string returnResult="";
        for (int j=0; j < sortResult.Length; j++)
        {

            returnResult += sortResult[j];
        
        }
        Response.Write("结果为:"+returnResult);
        Session["CheckCode"] = returnResult;//可以在提交窗体时用于区别输入是否正确
        DrawImage(Session["CheckCode"].ToString());

        

    }
void DrawImage(string Code) {
        Bitmap checkImage = new Bitmap(100,50);
        
        Graphics g = Graphics.FromImage(checkImage);
        g.Clear(Color.White);//清空checkimage的内容并设置图片的默认背景
      
        //定义颜色
        Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
        //定义字体 
        string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
        Random num=new Random();
       
        for(int i = 0; i < Code.Length; i++)
        {
            int fIndex=num.Next(5);
            int cIndex=num.Next(8);

            Font f = new Font(font[fIndex], 12, FontStyle.Bold);//定义画笔
            Brush b = new SolidBrush(c[cIndex]);//定义画刷
            string content = Code.Substring(i, 1);
            g.DrawString(content, f, b, (i + 1) * 12, 15);
                           
        }
        
        
        g.DrawRectangle(new Pen(Color.Red, 0), 0, 0, checkImage.Width - 1, checkImage.Height - 1);//画像框
        MemoryStream stream = new MemoryStream();
        checkImage.Save(stream, ImageFormat.Jpeg);
        Response.Clear();
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(stream.ToArray());
 
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics