【ASP.NET】生成随机验证码 ValidateCode

ASP.NET 专栏收录该内容
27 篇文章 0 订阅

源码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Common
{
    public class ValidateCode
    {
        /// <summary>
        /// 验证码长度 
        /// </summary>
        public int CodeLen { get; set; }
        /// <summary>
        /// 验证码
        /// </summary>
        public string VCode { get; private set; }
        /// <summary>
        /// 验证码图像
        /// </summary>
        public Bitmap VCodeImg { get; private set; }

        private int fineness = 70;//图片清晰度  数值越大越清晰
        private int imgWidth = 48;//图片宽度
        private int imgHeight = 24;//图片高度 
        private int fontSize = 14;//字体大小
        private string fontFamily = "Microsoft YaHei";//字体名称

        /// <summary>
        /// 生成验证码 
        /// </summary>
        public string CreateVCode(int codeLen = 4)
        {
            CodeLen = codeLen;

            string vCode = "";
            Random random = new Random();
            for (int i = 0; i < CodeLen; i++)
            {
                int n = random.Next(10);
                vCode += n.ToString();
            }
            VCode = vCode;
            return vCode;
        }

        /// <summary>
        /// 验证码图像Byte
        /// </summary>
        /// <returns></returns>
        public byte[] GetCodeImgByte()
        {
            VCodeImg = new Bitmap(imgWidth, imgHeight);
            //生成像素点
            DrawPixel(VCodeImg);
            //绘制验证码图像
            DrewVCode(VCodeImg, VCode);
            //Bitmap转byte[]
            byte[] imgByte = BitmapToByte(VCodeImg);

            return imgByte;
        }

        /// <summary>
        /// 生成像素点
        /// </summary>
        private void DrawPixel(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) <= fineness)
                        bitmap.SetPixel(i, j, Color.LightGray);
                }
            }
        }

        /// <summary>
        /// 绘制验证码图像 
        /// </summary>
        /// <param name="bitmap"></param>
        private void DrewVCode(Bitmap bitmap,string vCode)
        {
            Graphics g = Graphics.FromImage(bitmap);
            Font font = new Font(fontFamily, fontSize, FontStyle.Regular);
            g.DrawString(vCode, font, Brushes.Black, 0, 0);
        }

        /// <summary>
        /// Bitmap转byte[]
        /// </summary>
        private byte[] BitmapToByte(Bitmap bitmap)
        {
            MemoryStream ms = new MemoryStream();
            bitmap.Save(ms, ImageFormat.Bmp);
            byte[] imgByte = ms.GetBuffer();
            return imgByte;
        }
    }
}

使用

Common.ValidateCode validateCode = new Common.ValidateCode();

//生成验证码
string vCode = validateCode.CreateVCode();

//生成验证码图像Byte
byte[] imgByte = validateCode.GetCodeImgByte();

//获取验证码图像
Bitmap bitmap = validateCode.VCodeImg;

效果

  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值