zoukankan      html  css  js  c++  java
  • 设计用户登陆图片验证码 dodo

    实现步骤:
    1.服务器随机产生一个长度为N(N值可以由程序设置)的验证码字符串,该字符串可以含数字,大小写字母等。
    2.创建一张图片,供显示验证码字符串。
    3.在图片上显示验证字符串
    当用户刷新页面的时候,每次创建的字符串是不同的,在浏览器端,用户输入图片上的字符串,然后提交到服务器端,比较由用户提交的字符串和服务器端保存的该验证嘛的字符串是否一致,如果一致就继续,否则返回提示信息。

    eg.
    //创建一个随机数
    private int GetRandomint(int iMin,int iMax)
    {
        Random random = new Random();
        return(random.Next(iMin,iMax);
    }

    //创建所有字符,为创建验证字符串做准备
    private void InitLetterList()
    {
        for(int i = 0 ; i < 10 ;  i++)
        {
            //添加数字
            LetterList.Append(i.ToString());
        }

        for(int i = 0 ; i < 26 ;  i++)
        {
            //添加大写字符
            LetterList.Append(((char)((int)'A' + i)).ToString());
        }

        for(int i = 0 ; i < 26 ;  i++)
        {
            //添加小写字符
            LetterList.Append(((char()((int)'a' + i)).ToString());
        }
       
    }

    //创建验证字符串
    private string CreateValidateString(int nLen)
    {
        //初始化
        InitLetterList();
        //创建一个StringBuilder()对象
        StringBuilder sb = new StringBuilder(nLen);
        for(int i = 0 ; i  < nLen ; i++)
        {
        //随机产生一个字符
        int index = GetRandomint(0,LetterList.Length -1);
        sb.Append(LetterList[index].ToString());
        LetterList.Remove(index,1);
        }
       //返回验证码字符串
       return(sb.ToString());
       
    }

    //为使验证码字符串难以辨认,在输出验证码字符串时,随机设置了字符串的FontStyle属性,和Brush属性

    //根据随机数产生获取FontSytle属性的枚举值
    private int CreateRandomFontStyle(int random)
    {
        if(random < 200){return 0};
        if(random < 400){return 1};
         if(random < 600){return 2};
        if(random < 800){return 3};
         if(random < 1000){return 4};
        return 4;
    }

    //初始化Brush列表
    private void InitBrushList()
    {
        //添加Brush
        BrushList[0] = SystemBrushes.ActiveBorder;
        ....
        BrushList[31] = SystemBrushes.WindowText;
    }

    //页面创建并显示验证码字符串
    1.调用InitBrushList()初始化保存Brush数组BrushList
    2.获取服务器端产生的验证码字符串
    3.创建一个BMP位图
    4.在BMP位图上输出验证码字符串
    5.设置输出格式,并输出页面
    private readonly string ImagePath = "images/Validator.jpg";
    private string sValidator = "";
    private Brush[] BrushList = new Brush[32];

    pageload方法
    {
        //初始化
        InitBrushList();
        if(Request.Params["Validator"] != null)
        {
            //获取验证字符串
            sValidator = Request.Params["Validator"].ToString();
        }
        //创建BMP位图
       Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath(ImagePath);
       Graphics graphicImage = Graphics.FromImage(bitMapImage);

       ///设置画笔的输出模式
            graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
            ///添加文本字符串
      for(int i = 0; i < sValidator.Length; i++)
      {
       graphicImage.DrawString(sValidator[i].ToString(),
        new Font("Arial",20,(FontStyle)CreateRandomFontStyle(GetRandomint(0,1000))),
        BrushList[GetRandomint(0,BrushList.Length - 1)],
        new PointF(i * 15,GetRandomint(-5,5)));
      }

      //graphicImage.DrawString(sValidator, new Font("Arial", 20, (FontStyle)GetRandomint(0,4)),SystemBrushes.WindowText, new Point(0, 0));

            ///设置图像输出的格式
            Response.ContentType = "image/jpeg";

            ///保存数据流
            bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg);

            ///释放占用的资源
            graphicImage.Dispose();
            bitMapImage.Dispose();
      Response.End();
        }

  • 相关阅读:
    locate命令详解
    python 爬虫获取文件式网站资源完整版(基于python 3.6)
    python 爬虫获取文件式网站资源(基于python 3.6)
    利用python 获取网址中的href(基于python 3.6)
    springmvc+font-awesome开发出的页面显示方框乱码的解决方法
    2017年6月短学期培训代码总结 -----springMvc
    2017年6月短学期培训代码总结 -----JDBC
    构建之法 第十章 典型用户和场景
    构建之法 第九章 项目经理
    构建之法 第八章 需求分析
  • 原文地址:https://www.cnblogs.com/zgqys1980/p/540769.html
Copyright © 2011-2022 走看看