C#实现验证码编写


    视频看到最后验证码的编写又激发了我的兴趣,想象在窗体登录时,加上验证码是不是更显得正规一点。


namespace 验证码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void picString_Click(object sender, EventArgs e)
        {
            Random r = new Random();

            string strNumber = "";

            for (int i = 0; i < 5; i++)
            {
                int num = r.Next(10);
                //随机数存入字符串
                strNumber += num;
                
            }

          
            //创建一个位图用来存验证码
            Bitmap bm = new Bitmap(110, 30);
            //创建这个对象
            Graphics g = Graphics.FromImage(bm);
            //画验证码

            // 五种字体 
            string[] fontName = new string[] {"幼圆","全新硬笔行书简","华文彩云","宋体","方正舒体" };

            //五种颜色
            Color[] color = new Color[] { Color.Red,Color.Blue,Color.Green,Color.Pink,Color.Black};

            for (int i = 0; i < 5; i++)
            {
                Point point=new Point(i*20,0);
                g.DrawString(strNumber[i].ToString(), new Font(fontName[i], 20), new SolidBrush(color[i]),point);
            }
            //画线
            for (int i = 0; i < 25; i++)
            {

                Point p_1 = new Point(r.Next(bm.Width), r.Next(bm.Height));

                Point p_2 = new Point(r.Next(bm.Width), r.Next(bm.Height));
                g.DrawLine(new Pen(Brushes.Gray),p_1,p_2);
            }

            //画点
            for (int i = 0; i < 50; i++)
            {
                 Point p_1 = new Point(r.Next(bm.Width), r.Next(bm.Height));
                 bm.SetPixel(p_1.X, p_1.Y, Color.Yellow);
            }
            picString.Image = bm;

        }
    }
}

评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值