C#学习之自动生成四位验证码程序

任务描述

要求设计一个Winform窗体应用程序,进入程序后显示第一个窗体及相应的验证码,验证码可以刷新。然后允许用户输入进行验证。如果验证成功则跳到窗体2,否则报错。

窗体设计

主要是对Form1进行设计,Form2只是检验Form1的功能。
Form1包括:两个Label控件,一个TextBox控件(用于用户输入验证码),一个PictureBox控件(用于程序显示验证码),两个Button控件(Button1表示确认,Button2表示刷新)。
Form1设计界面:
Form1设计
Form2设计界面:
在这里插入图片描述

用到的主要类及方法

自己写两个方法:

  1. CheckBox():用于生成四位的验证码。
  2. CodeImage():用于在PictureBox控件上绘制验证码。

用到的类与方法:

  1. Random类,random.Next().
  2. (char)强制类型转换。
  3. Bitmap类及其创造函数。bitmap.SetPixel():画点
  4. Graphics类,创建:Graphics.FromImage()。grahics.DrawLine():画线。
  5. grahics.DrawString():画字符串。
  6. grahics.DrawRectangle(): 画边框线矩形。
  7. 设置PictureBox.BackgroundImage= imit.
  8. showdialog, this.dispose(): 窗体切换的方法。

主程序代码

Form1的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        private string checkcode;
        public Form1()
        {
            InitializeComponent();
        }

        private string CheckBox()  //生成所需的验证码
        {
            int number;
            char code;
            Random random = new Random();
            this.checkcode = String.Empty;
            string showcheckcode = String.Empty;
            for(int i=0;i<4;i++)
            {
                number = random.Next(36);
                if(number>10)
                {
                    code = (char)('A' + (char)((number-9) % 26));
                }
                else
                {
                    code = (char)('0' + (char)(number));
                }
                showcheckcode += " " + code.ToString();
                this.checkcode += code.ToString();
            }
            return showcheckcode;
        }
        private void CodeImage(string showcheckcode)  //显示验证码图片
        {
            /*所需要的操作包括:
             * 1.画背景噪音线
             * 2.画验证码字符串
             * 3.画前景噪音点
             * 4.画边框线
             */
            if (showcheckcode == null || showcheckcode.Trim() == String.Empty) return;
            Bitmap imit = new Bitmap((int)Math.Ceiling((showcheckcode.Length*10.5)), 20);
            Graphics g = Graphics.FromImage(imit);
            Random random = new Random();  //生成随机生成器对象
            g.Clear(Color.White); //清空图片背景色
            //画背景噪音线
            for(int i=0;i<3; i++)
            {
                int x1 = random.Next(imit.Width);
                int x2 = random.Next(imit.Width);
                int x3 = random.Next(imit.Width);
                int y1 = random.Next(imit.Height);
                int y2 = random.Next(imit.Height);
                int y3 = random.Next(imit.Height);
                g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
            }
            //画验证码字符串
            Font font = new Font("Arial", 12, (FontStyle.Bold));
            g.DrawString(showcheckcode, font, new SolidBrush(Color.Red), 2, 2);
            //画前景噪声点
            for(int i=0;i<100;i++)
            {
                int x3 = random.Next(imit.Width);
                int y1 = random.Next(imit.Height);
                imit.SetPixel(x3, y1, Color.FromArgb(random.Next()));
            }
            //画边框线
            g.DrawRectangle(new Pen(Color.Blue), 0,0, imit.Width - 1, imit.Height - 1);
            this.pictureBox1.Width = imit.Width;
            this.pictureBox1.Height = imit.Height;
            this.pictureBox1.BackgroundImage = imit;
        }
        private void Form1_Load(object sender, EventArgs e)

        {
            CodeImage(CheckBox());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CodeImage(CheckBox());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(this.checkcode==textBox1.Text)
            {
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
                this.Hide();
                this.Dispose();
            }
            else
            {
                MessageBox.Show("验证码错误,请重新输入!");
                CodeImage(CheckBox());
            }
        }


    }

}

Form2的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp5
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form1 frm1 = new Form1();
            frm1.ShowDialog();
            this.Dispose();
            
        }
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值