验证码制作

登录界面制作验证码效果
验证码效果图:
在这里插入图片描述
验证码效果源代码:
///
/// 生成验证码字符串
///
/// 验证码字符长度
/// 返回验证码字符串
private string MakeCode(int codeLen)
{
if (codeLen < 1)
{
return string.Empty;
}

        int number;
        string checkCode = string.Empty;
        Random random = new Random();

        for (int index = 0; index < codeLen; index++)
        {

            number = random.Next();

            if (number % 2 == 0)
            {
                checkCode += (char)('0' + (char)(number % 10));     //生成数字
            }
            else
            {
                checkCode += (char)('A' + (char)(number % 26));     //生成字母
            }
        }

        return checkCode;
    }


    ///<summary>
            /// 获取验证码图片流
            /// </summary>
            /// <param name="checkCode">验证码字符串</param>
            /// <returns>返回验证码图片流</returns>
    private Image CreateCodeImg(string checkCode)
    {
        if (string.IsNullOrEmpty(checkCode))
        {
            return null;
        }

        Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);

        Graphics graphic = Graphics.FromImage(image);

        try
        {
            Random random = new Random();

            graphic.Clear(Color.White);

            int x1 = 0, y1 = 0, x2 = 0, y2 = 0;

            for (int index = 0; index < 25; index++)
            {
                x1 = random.Next(image.Width);
                x2 = random.Next(image.Width);
                y1 = random.Next(image.Height);
                y2 = random.Next(image.Height);

                graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

            Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
            graphic.DrawString(checkCode, font, brush, 2, 2);

            int x = 0;
            int y = 0;

            //画图片的前景噪音点
            for (int i = 0; i < 100; i++)
            {
                x = random.Next(image.Width);
                y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }

            //画图片的边框线
            graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

            return image;

        }
        finally
        {
            graphic.Dispose();
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML5本身并不支持验证码制作,但是可以通过以下步骤使用HTML5制作验证码: 1. 使用HTML5的canvas标签创建一个画布。 2. 使用JavaScript生成随机的验证码字符串。 3. 在画布上绘制验证码字符串。 4. 在画布上绘制一些干扰线或干扰点,使验证码更难以被机器破解。 5. 将画布作为图片显示在页面上,作为验证码。 以下是一个简单的HTML5验证码制作示例: ``` <!DOCTYPE html> <html> <head> <title>HTML5验证码制作</title> <script type="text/javascript"> function createCaptcha() { var canvas = document.getElementById("captcha"); var ctx = canvas.getContext("2d"); var captcha = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 6; i++) { captcha += possible.charAt(Math.floor(Math.random() * possible.length)); } ctx.font = "30px Arial"; ctx.fillText(captcha, 10, 40); for (var i = 0; i < 10; i++) { ctx.beginPath(); ctx.moveTo(Math.random() * canvas.width, Math.random() * canvas.height); ctx.lineTo(Math.random() * canvas.width, Math.random() * canvas.height); ctx.strokeStyle = '#ddd'; ctx.stroke(); } for (var i = 0; i < 100; i++) { ctx.beginPath(); ctx.arc(Math.random() * canvas.width, Math.random() * canvas.height, 1, 0, 2 * Math.PI); ctx.fillStyle = '#ddd'; ctx.fill(); } document.getElementById("captchaInput").value = captcha; } </script> </head> <body onload="createCaptcha();"> <h1>HTML5验证码制作</h1> <canvas id="captcha" width="200" height="50"></canvas> <br> <input type="text" id="captchaInput"> <button onclick="createCaptcha();">刷新验证码</button> </body> </html> ``` 在上面的示例中,我们使用了HTML5的canvas标签创建了一个画布,并使用JavaScript生成了一个6位的随机验证码字符串。然后使用ctx.fillText()方法将验证码字符串绘制到画布上,使用ctx.beginPath()、ctx.moveTo()、ctx.lineTo()、ctx.arc()等方法绘制了一些干扰线和干扰点,最后将生成的验证码字符串设置到一个文本框中。用户输入验证码后,可以通过JavaScript验证用户输入的验证码是否正确。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值