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("");//清空验证码输入框内容
            });
        }

 

转载于:https://www.cnblogs.com/lql6/p/7655691.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值