小項目:生成類似QQ賬號密碼登陸那樣的驗證碼(基於C#、Winform)

 

運行結果:

代碼:

1) VS2015 新建一個WindowsFormsApplication工程,

2) 在Form1.cs中,添加如下控件:

表 窗體及控件屬性設置

窗體和控件

屬性

屬性值

Form1

Text

驗證碼

pictureBox1

Name

pictureBoxCode

textBox1

Name

txtCode

button1

Name

btnCheck

Text

驗證

3) 鼠標右擊Form1.cs 找到對應的代碼工程,代碼部分參考如下代碼:

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 WinFormsVerificationCode
{
    public partial class Form1 : Form
    {
        //在主窗體中定義全局變量
        string randomCode = string.Empty;

        //方法:生成隨機字符串
        private string CreateRandomCode(int codeCount)
        {
            string randomCode = "";
            string allchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
            string[] allCharArray = allchar.Split(',');//分割 生成子串
            Random rand = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                //生成隨機數 且 該隨機數<=allCharArray.Length
                randomCode += allCharArray[rand.Next(allCharArray.Length)].ToString();
            }
            return randomCode;
        }

        //方法:生成驗證碼圖像
        private Bitmap CreateImage(string code, int fontSize, string font, Color foreColor, Color bgColor)
        {
            int imageWidth = (code.Length * fontSize) + 40;//圖片寬度
            int imageHeight = fontSize * 2 + 10;//圖片高度
            Bitmap image = new Bitmap(imageWidth, imageHeight);//生成圖片框

            Graphics g = Graphics.FromImage(image);
            g.Clear(bgColor);

            Font f = new Font(font, fontSize, FontStyle.Bold);
            //使用指定的畫筆和字體對象在指定位置繪製指定的文本字符串。
            g.DrawString(code, f, new SolidBrush(ForeColor), 1, 1);//設置驗證碼

            Random rnd = new Random();
            //給背景添加隨機生成的燥點
            for (int i = 0; i < 100; i++)
            {
                int x = rnd.Next(imageWidth);
                int y = rnd.Next(imageHeight);
                g.FillRectangle(new SolidBrush(Color.FromArgb(rnd.Next())), x, y, 2, 2);
            }
            //給背景添加隨機生成的干擾線
            for (int i = 0; i < 50; i++)
            {
                int x1 = rnd.Next(imageWidth);
                int x2 = rnd.Next(imageWidth);
                int y1 = rnd.Next(imageHeight);
                int y2 = rnd.Next(imageHeight);
                g.DrawLine(new Pen(Color.FromArgb(rnd.Next()), 2), x1, y1, x2, y2);
            }
            return image;
        }

        //方法:刷新驗證碼
        private void freshCode()
        {
            randomCode = CreateRandomCode(5);
            pictureBoxCode.Image = CreateImage(randomCode, 35, "宋體", Color.Blue, Color.White);
        }

        public Form1()
        {
            InitializeComponent();
        }

        //按鈕點擊事件處理程序
        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (randomCode.ToUpper() != txtCode.Text.ToUpper())
            {
                MessageBox.Show("驗證碼輸入錯誤,請重新輸出!");
                txtCode.Text = "";
                freshCode();
            }
            else
            {
                MessageBox.Show("恭喜,驗證碼輸入正確!");
            }
        }

        //窗體加載事件處理程序
        private void Form1_Load(object sender, EventArgs e)
        {
            freshCode();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章