zoukankan      html  css  js  c++  java
  • C#制作登录验证码

    后台代码:

     1 public ActionResult VerificationCode()
     2         {
     3             try
     4             {
     5                 int _verificationLength = 4;//密码长度
     6                 int _width = 100, _height = 50;
     7                 SizeF _verificationTextSize;
     8                 Random r = new Random();
     9                 Bitmap _bitmap = new Bitmap(Server.MapPath("~/Common/Texture" + (int)r.Next(0, 3) + ".jpg"), true);//此处用的图片
    10                 TextureBrush _brush = new TextureBrush(_bitmap);
    11                 //获取验证码
    12                 string _verificationText = ValidateCode.VerificationText(_verificationLength);
    13                 //存储验证码
    14                 Session["VerificationCode"] = _verificationText.ToUpper();
    15                 Font _font = new Font("Arial", 14, FontStyle.Bold);
    16                 Bitmap _image = new Bitmap(_width, _height);
    17                 Graphics _g = Graphics.FromImage(_image);
    18                 //清空背景色
    19                 _g.Clear(Color.White);
    20                 //绘制验证码
    21                 _verificationTextSize = _g.MeasureString(_verificationText, _font);
    22                 _g.DrawString(_verificationText, _font, _brush, (_width - _verificationTextSize.Width) / 2, (_height - _verificationTextSize.Height) / 2);
    23                 _image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    24                 HttpContext.Response.Write(_image);
    25             }
    26             catch (Exception)
    27             {
    28 
    29             }
    30             return null;
    31         }

    前端代码:

    1 <td>
    2    <img src="" class="add-on" id="vcode" title="点击更换验证码" style="float:left;clear:both;line-height:12px;border-radius:4px;" />
    3    <a class="span1 pull-left" style="margin-top:4px;" onclick="getValidateCode();">点击图片可更换验证码</a>
    4  </td>

    JS代码:

    function getValidateCode() {
                //获取验证码
                $("img#vcode").attr("src", "/manager/Login/VerificationCode?" + new Date());
                $("img#vcode").click(function () {
                    $(this).attr("src", "/manager/Login/VerificationCode?" + new Date());
                    $("#vcode").val("");//清空验证码输入框内容
                });
            }
  • 相关阅读:
    🔥低代码音视频开发训练营火热报名中!
    编解码再进化:Ali266 与下一代视频技术
    ICCV 2021口罩人物身份鉴别全球挑战赛冠军方案分享
    提升 RTC 音频体验 从搞懂硬件开始
    只要你有目标,只要你肯努力,成功只是时间问题
    安全感到底来自何方
    工作经验小结
    女人的出路在何方?
    那些以为过去了的
    初出茅庐
  • 原文地址:https://www.cnblogs.com/lql6/p/7655691.html
Copyright © 2011-2022 走看看