C#绘制验证码

本文介绍如何使用C#语言生成包含随机数字和字母的验证码图片,详细讲解了验证码的绘制过程,包括颜色选择、字体变形、噪点添加等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

		/// <summary>
        /// 绘制验证码的方法
        /// </summary>
        void SetCode()
        {
            //默认验证码
            string code = "2wa4";
            code = code.Trim();
            if (String.IsNullOrEmpty(code))
            {
                return;
            }
            //验证码大小宽65,高20
            Bitmap image = new Bitmap(65, 20);
            //创建绘图对象Graphics
            Graphics graphics = Graphics.FromImage(image);
            //随机
            Random r = new Random();
            graphics.Clear(Color.White);
            //绘制四条随机出现的线
            for (int i = 0; i < 4; i++)
            {
                int x1 = r.Next(image.Width);
                int y1 = r.Next(image.Height);

                int x2 = r.Next(image.Width);
                int y2 = r.Next(image.Height);
                graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(x1, y1), new Point(x2, y2));
            }
            this.BackgroundImage = image;
            this.BackgroundImageLayout = ImageLayout.Center;
            graphics.DrawRectangle(new Pen(Brushes.Black), 0, 0, image.Width - 1, image.Height - 1);
            graphics.DrawString(code, new Font("微软雅黑", 12, FontStyle.Bold), Brushes.Red, 8, -2);
        }

绘制随机出现数字加字幕的验证码
在这里插入图片描述

		Random r = new Random();
        /// <summary>
        /// 随机出现验证码
        /// </summary>
        /// <param name="code">传递验证码个数</param>
        /// <returns>返回验证码</returns>
        private string MakeCode(int code)
        {
            int number;
            StringBuilder CheckCode = new StringBuilder();
            for (int i = 0; i < code; i++)
            {
                number = r.Next();
                ///判断出现数字
                if (number % 2 == 0)
                {
                    CheckCode.Append((char)('0' +(number % 10)));
                }
                ///判断出现英文字母
                else
                {
                    CheckCode.Append((char)('A' +(number % 26)));
                }
            }
            return CheckCode.ToString();
        }
        /// <summary>
        /// 绘制验证码
        /// </summary>
        void SetCodeImage()
        {
            string code = MakeCode(4);
            if (String.IsNullOrEmpty(code))
            {
                return;
            }
            Bitmap image = new Bitmap((int)Math.Ceiling(code.Length * 20.0), 20);
            Graphics graphics = Graphics.FromImage(image);
           
            graphics.Clear(Color.White);
            for (int i = 0; i < 4; i++)
            {
                int x1 = r.Next(image.Width);
                int y1 = r.Next(image.Height);
                int x2 = r.Next(image.Width);
                int y2 = r.Next(image.Height);
                graphics.DrawLine(new Pen(Color.Black, 2), new Point(x1, y1), new Point(x2, y2));
            }
            graphics.DrawString(code, new Font("黑体", 18, FontStyle.Bold), Brushes.Red,8,-2);
            this.BackgroundImage = image;
            this.BackgroundImageLayout = ImageLayout.Center;
            graphics.DrawRectangle(new Pen(Color.Black), 0, 0, image.Width-1, image.Height-1);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值