C#實(shí)現(xiàn)中文驗(yàn)證碼的示例代碼
更新時(shí)間:2022年12月12日 10:52:19 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)中文驗(yàn)證碼功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下
實(shí)踐過程
效果

代碼
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string txt = "";
private void Form1_Load(object sender, EventArgs e)
{
CreateImage();
}
private void CreateImage()
{
//獲取GB2312編碼頁(表)
Encoding gb = Encoding.GetEncoding("gb2312");
//調(diào)用函數(shù)產(chǎn)生4個(gè)隨機(jī)中文漢字編碼
object[] bytes = CreateCode(4);
//根據(jù)漢字編碼的字節(jié)數(shù)組解碼出中文漢字
string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
txt = str1 + str2 + str3 + str4;
if (txt == null || txt == String.Empty)
{
return;
}
Bitmap image = new Bitmap((int)Math.Ceiling((txt.Length * 20.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成隨機(jī)生成器
Random random = new Random();
//清空圖片背景色
g.Clear(Color.White);
//畫圖片的背景噪音線
for (int i = 0; i < 2; i++)
{
Point tem_Point_1 = new Point(random.Next(image.Width), random.Next(image.Height));
Point tem_Point_2 = new Point(random.Next(image.Width), random.Next(image.Height));
g.DrawLine(new Pen(Color.Black), tem_Point_1, tem_Point_2);
}
Font font = new Font("宋體", 12, (FontStyle.Bold));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(txt, font, brush, 2, 2);
//畫圖片的前景噪音點(diǎn)
for (int i = 0; i < 100; i++)
{
Point tem_point = new Point(random.Next(image.Width),random.Next(image.Height));
image.SetPixel(tem_point.X,tem_point.Y, Color.FromArgb(random.Next()));
}
//畫圖片的邊框線
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
pictureBox1.Image = image;
}
catch { }
}
/**/
/*
此函數(shù)在漢字編碼范圍內(nèi)隨機(jī)創(chuàng)建含兩個(gè)元素的十六進(jìn)制字節(jié)數(shù)組,每個(gè)字節(jié)數(shù)組代表一個(gè)漢字,并將
四個(gè)字節(jié)數(shù)組存儲在object數(shù)組中。
參數(shù):strlength,代表需要產(chǎn)生的漢字個(gè)數(shù)
*/
public static object[] CreateCode(int strlength)
{
//定義一個(gè)字符串?dāng)?shù)組儲存漢字編碼的組成元素
string[] r=new String [16]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
Random rnd=new Random();
//定義一個(gè)object數(shù)組用來
object[] bytes=new object[strlength];
/**//*每循環(huán)一次產(chǎn)生一個(gè)含兩個(gè)元素的十六進(jìn)制字節(jié)數(shù)組,并將其放入bject數(shù)組中
每個(gè)漢字有四個(gè)區(qū)位碼組成
區(qū)位碼第1位和區(qū)位碼第2位作為字節(jié)數(shù)組第一個(gè)元素
區(qū)位碼第3位和區(qū)位碼第4位作為字節(jié)數(shù)組第二個(gè)元素
*/
for(int i=0;i<strlength;i++)
{
//區(qū)位碼第1位
int r1=rnd.Next(11,14);
string str_r1 = r[r1].Trim();
//區(qū)位碼第2位
rnd=new Random(r1*unchecked((int)DateTime.Now.Ticks)+i);//更換隨機(jī)數(shù)發(fā)生器的種子避免產(chǎn)生重復(fù)值
int r2;
if (r1==13)
r2=rnd.Next(0,7);
else
r2=rnd.Next(0,16);
string str_r2 = r[r2].Trim();
//區(qū)位碼第3位
rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks)+i);
int r3=rnd.Next(10,16);
string str_r3 = r[r3].Trim();
//區(qū)位碼第4位
rnd=new Random(r3*unchecked((int)DateTime.Now.Ticks)+i);
int r4;
if (r3==10)
{
r4=rnd.Next(1,16);
}
else if (r3==15)
{
r4=rnd.Next(0,15);
}
else
{
r4=rnd.Next(0,16);
}
string str_r4 = r[r4].Trim();
//定義兩個(gè)字節(jié)變量存儲產(chǎn)生的隨機(jī)漢字區(qū)位碼
byte byte1=Convert.ToByte(str_r1 + str_r2,16);
byte byte2=Convert.ToByte(str_r3 + str_r4,16);
//將兩個(gè)字節(jié)變量存儲在字節(jié)數(shù)組中
byte[] str_r=new byte[]{byte1,byte2};
//將產(chǎn)生的一個(gè)漢字的字節(jié)數(shù)組放入object數(shù)組中
bytes.SetValue(str_r,i);
}
return bytes;
}
private void button2_Click(object sender, EventArgs e)
{
CreateImage();
}
private void button1_Click(object sender, EventArgs e)
{
if (txtCode.Text.Trim() == "")
return;
else
{
if (txtCode.Text.Trim() == txt)
{
MessageBox.Show("提示:驗(yàn)證碼輸入正確!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
MessageBox.Show("提示:驗(yàn)證碼輸入錯(cuò)誤,請重新輸入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
partial class Form1
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.txtCode = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(192, 18);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(76, 21);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(77, 12);
this.label1.TabIndex = 1;
this.label1.Text = "輸入驗(yàn)證碼:";
//
// txtCode
//
this.txtCode.Location = new System.Drawing.Point(86, 18);
this.txtCode.Name = "txtCode";
this.txtCode.Size = new System.Drawing.Size(100, 21);
this.txtCode.TabIndex = 2;
//
// button1
//
this.button1.Location = new System.Drawing.Point(62, 58);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "確定";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(143, 58);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 4;
this.button2.Text = "刷新";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(283, 93);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtCode);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "中文驗(yàn)證碼";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtCode;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
到此這篇關(guān)于C#實(shí)現(xiàn)中文驗(yàn)證碼的示例代碼的文章就介紹到這了,更多相關(guān)C# 中文驗(yàn)證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity?UGUI的PointerEventData的介紹及使用
這篇文章主要為大家介紹了Unity?UGUI的PointerEventData的介紹及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
詳解C# Protobuf如何做到0分配內(nèi)存的序列化
這篇文章主要介紹了詳解C# Protobuf如何做到0分配內(nèi)存的序列化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
C#實(shí)現(xiàn)DataTable轉(zhuǎn)換成IList的方法
這篇文章主要介紹了C#實(shí)現(xiàn)DataTable轉(zhuǎn)換成IList的方法,涉及C#針對DataTable操作的相關(guān)技巧,需要的朋友可以參考下2016-03-03
C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘二 線性結(jié)構(gòu)
本文中,我們討論了什么是線性結(jié)構(gòu),線性結(jié)構(gòu)有哪些特點(diǎn),并且詳細(xì)介紹了一個(gè)最簡單線性結(jié)構(gòu)順序表,并且通過源代碼對她進(jìn)行一些列的分析,最后還舉了兩個(gè)例子,讓我們更好的理解順序表2012-11-11

