zoukankan      html  css  js  c++  java
  • 验证码

    方法一:点击图片实现更换验证码

    <img src="验证码界面路径" alt="看不清楚,换一张" id="img" onclick="img.src=验证码界面路径?'+new Date().getTime()";" />

    方法二:点超链接更换验证码、下面这个例子两个方法都用到了、点击图片也可以

    <script language="javascript">  
    function change()  
    {  
    var img =document.getElementById("codeimg");  
    img.src=img.src+"?";  
    }  
    </script>  
    <a href="javascript:change();">看不清,换一张 </a>  

    <img alt="" src="" style="cursor:pointer;" id="codeimg" onclick="this.src='validate.aspx?time=' + Math.random();" /> <script language="javascript" type="text/javascript">document.getElementById('codeimg').src = 'validate.aspx?time=' + Math.random(); </script>  

    三、

    protected void Page_Load(object sender, EventArgs e)
    {
    string validateCode = CreateValidateCode();//生成验证码
    Bitmap bitmap = new Bitmap(imgWidth, imgHeight);//生成Bitmap图像
    DisturbBitmap(bitmap); //图像背景
    DrewValidateCode(bitmap, validateCode);//绘制验证码图像
    bitmap.Save(Response.OutputStream, ImageFormat.Gif);//保存图像,等待输出
    }
    private int codeLen = 4;//验证码长度
    private int fineness = 85;//图片清晰度
    private int imgWidth = 48;//图片宽度
    private int imgHeight = 24;//图片高度
    private string fontFamily = "Times New Roman";//字体名称
    private int fontSize = 10;//字体大小
    private int fontStyle = 0;//字体样式
    private int posX = 0;//绘制起始坐标X
    private int posY = 0;//绘制坐标Y
    private string CreateValidateCode() //生成验证码
    {
    string[] source = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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" };
    string validateCode = "";
    Random random = new Random();// 随机数对象
    for (int i = 0; i < codeLen; i++)//循环生成每位数值
    {
    string n =source[random.Next(0,source.Length)] ;//数字
    validateCode += n.ToString();
    }
    Session["vcode"] = validateCode;//保存验证码
    return validateCode;// 返回验证码
    }
    private void DisturbBitmap(Bitmap bitmap)//图像背景
    {
    Random random = new Random();//通过随机数生成
    for (int i = 0; i < bitmap.Width; i++)//通过循环嵌套,逐个像素点生成
    {
    for (int j = 0; j < bitmap.Height; j++)
    {
    if (random.Next(90) <= this.fineness)
    bitmap.SetPixel(i, j, Color.LightGray);
    }
    }
    }
    private void DrewValidateCode(Bitmap bitmap, string validateCode)//绘制验证码图像
    {
    Graphics g = Graphics.FromImage(bitmap);//获取绘制器对象
    Font font = new Font(fontFamily, fontSize, FontStyle.Bold);//设置绘制字体
    g.DrawString(validateCode, font, Brushes.Black, posX, posY);//绘制验证码图像
    }

  • 相关阅读:
    Spring Boot 自定义属性 以及 乱码问题
    IDEA 修改文件编码
    Gojs简单例子
    无法转换json问题 Error: Model.nodeDataArray value is not an instance of Array or NodeList or HTMLCollection
    java json转换
    git设置HTTP代理
    thymeleaf中的日期格式化
    thymeleaf:字符串Strings常见的使用方法
    thymeleaf+bootstrap,onclick传参实现模态框中遇到的错误
    Thymeleaf教程 (十二) 标签内,js中使用表达式
  • 原文地址:https://www.cnblogs.com/nzgbk/p/5770166.html
Copyright © 2011-2022 走看看