閑來無事挠锥,最近自己發(fā)現(xiàn)自己的驗證碼功能還沒有寫過十兢。于是就寫下了這篇文章。
界面就比較丑了耘擂,一個picturebox胆剧,一個textbox,一個button按鈕主要想的是先把功能實現(xiàn)了醉冤,萬一以后業(yè)務(wù)上需要使用呢秩霍。
實現(xiàn)以后的功能圖
在文本框中輸入對應(yīng)文字,點擊確定來驗證冤灾,正確時候如圖所示
如果驗證失敗前域,沒有提示,直接更新驗證碼韵吨,當(dāng)然需要使用的時候根據(jù)業(yè)務(wù)邏輯來就是了匿垄,這個就比較簡單了。
第一:生成驗證碼字符串归粉,用到的是Random隨機(jī)函數(shù)
第二:將該字符串畫在picturebox中
第三點擊圖片椿疗,刷新驗證碼
第四驗證驗證碼不區(qū)分大小寫
或者區(qū)分大小寫
此時完成
源碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace suijima
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? //驗證碼的長度
? ? ? ? private const int iVerifyCodeLength = 6;
? ? ? ? //驗證碼
? ? ? ? private String strVerifyCode = "";
? ? ? ? //匹配字符的臨時變量
? ? ? ? string strTemp = "";
? ? ? ? private void btnUpdate_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? UpdateVerifyCode();
? ? ? ? }
? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? UpdateVerifyCode();
? ? ? ? }
? ? ? ? //更新驗證碼
? ? ? ? private void UpdateVerifyCode()
? ? ? ? {
? ? ? ? ? ? strVerifyCode = CreateRandomCode(iVerifyCodeLength);
? ? ? ? ? ? if(strVerifyCode=="")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? strTemp = strVerifyCode;
? ? ? ? ? ? CreateImage(strVerifyCode);
? ? ? ? }
? ? ? ? //生成驗證碼字符串
? ? ? ? private string CreateRandomCode(int iLength)
? ? ? ? {
? ? ? ? ? ? int rand;
? ? ? ? ? ? char code;
? ? ? ? ? ? string randomCode = String.Empty;
? ? ? ? ? ? //生成一定長度的驗證碼
? ? ? ? ? ? System.Random random = new Random();
? ? ? ? ? ? for (int i = 0; i < iLength; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? rand = random.Next();
? ? ? ? ? ? ? ? if (rand % 3 == 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? code = (char)('A' + (char)(rand % 26));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? code = (char)('0' + (char)(rand % 10));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? randomCode += code.ToString();
? ? ? ? ? ? }
? ? ? ? ? ? return randomCode;
? ? ? ? }
? ? ? ? ///? 創(chuàng)建驗證碼圖片
? ? ? ? private void CreateImage(string strVerifyCode)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int iRandAngle = 45;? ? //隨機(jī)轉(zhuǎn)動角度
? ? ? ? ? ? ? ? int iMapWidth = (int)(strVerifyCode.Length * 21);
? ? ? ? ? ? ? ? Bitmap map = new Bitmap(iMapWidth, 28);? ? //創(chuàng)建圖片背景
? ? ? ? ? ? ? ? Graphics graph = Graphics.FromImage(map);
? ? ? ? ? ? ? ? graph.Clear(Color.AliceBlue);//清除畫面,填充背景
? ? ? ? ? ? ? ? graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - 1, map.Height - 1);//畫一個邊框
? ? ? ? ? ? ? ? graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//模式
? ? ? ? ? ? ? ? Random rand = new Random();
? ? ? ? ? ? ? ? //背景噪點生成
? ? ? ? ? ? ? ? Pen blackPen = new Pen(Color.LightGray, 0);
? ? ? ? ? ? ? ? for (int i = 0; i < 50; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? int x = rand.Next(0, map.Width);
? ? ? ? ? ? ? ? ? ? int y = rand.Next(0, map.Height);
? ? ? ? ? ? ? ? ? ? graph.DrawRectangle(blackPen, x, y, 1, 1);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //驗證碼旋轉(zhuǎn)糠悼,防止機(jī)器識別
? ? ? ? ? ? ? ? char[] chars = strVerifyCode.ToCharArray();//拆散字符串成單字符數(shù)組
? ? ? ? ? ? ? ? //文字距中
? ? ? ? ? ? ? ? StringFormat format = new StringFormat(StringFormatFlags.NoClip);
? ? ? ? ? ? ? ? format.Alignment = StringAlignment.Center;
? ? ? ? ? ? ? ? format.LineAlignment = StringAlignment.Center;
? ? ? ? ? ? ? ? //定義顏色
? ? ? ? ? ? ? ? Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green,
Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
? ? ? ? ? ? ? ? //定義字體
? ? ? ? ? ? ? ? string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" };
? ? ? ? ? ? ? ? for (int i = 0; i < chars.Length; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? int cindex = rand.Next(7);
? ? ? ? ? ? ? ? ? ? int findex = rand.Next(5); Font f = new System.Drawing.Font(font[findex], 13, System.Drawing.FontStyle.Bold);//字體樣式(參數(shù)2為字體大小)
? ? ? ? ? ? ? ? ? ? Brush b = new System.Drawing.SolidBrush(c[cindex]);
? ? ? ? ? ? ? ? ? ? Point dot = new Point(16, 16);
? ? ? ? ? ? ? ? ? ? float angle = rand.Next(-iRandAngle, iRandAngle);//轉(zhuǎn)動的度數(shù)
? ? ? ? ? ? ? ? ? ? graph.TranslateTransform(dot.X, dot.Y);//移動光標(biāo)到指定位置
? ? ? ? ? ? ? ? ? ? graph.RotateTransform(angle);
? ? ? ? ? ? ? ? ? ? graph.DrawString(chars[i].ToString(), f, b, 1, 1, format);
? ? ? ? ? ? ? ? ? ? graph.RotateTransform(-angle);//轉(zhuǎn)回去
? ? ? ? ? ? ? ? ? ? graph.TranslateTransform(2, -dot.Y);//移動光標(biāo)到指定位置
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? pictureBox1.Image = map;
? ? ? ? ? ? }
? ? ? ? ? ? catch (ArgumentException)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("創(chuàng)建圖片錯誤届榄。");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //驗證大小寫
? ? ? ? ? ? ? ? char[] ch1 = textBox1.Text.ToCharArray();
? ? ? ? ? ? ? ? char[] ch2 = strTemp.ToCharArray();
? ? ? ? ? ? ? ? int nCount = 0;
? ? ? ? ? ? ? ? for (int i = 0; i < strTemp.Length;i++ )
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if((ch1[i]>='a'&&ch1[i]<='z')||(ch1[i]>='A'&&ch1[i]<='Z'))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (ch1[i] - 32 == ch2[i] || ch1[i] + 32 == ch2[i])
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? nCount++;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (ch1[i]==ch2[i])
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? nCount++;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (nCount==strTemp.Length)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? MessageBox.Show("驗證通過");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? UpdateVerifyCode();
? ? ? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ////不能驗證大小寫
? ? ? ? ? ? //if(textBox1.Text==strTemp)
? ? ? ? ? ? //{
? ? ? ? ? ? //? ? MessageBox.Show("驗證通過");
? ? ? ? ? ? //}
? ? ? ? ? ? //else
? ? ? ? ? ? //{
? ? ? ? ? ? //? ? UpdateVerifyCode();
? ? ? ? ? ? //? ? textBox1.Text = "";
? ? ? ? ? ? //}
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 圖片點擊事件
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void pictureBox1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? UpdateVerifyCode();
? ? ? ? }
? ? }
}