C#實(shí)現(xiàn)中文驗(yàn)證碼的示例代碼
實(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編碼頁(yè)(表) 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(); //清空?qǐng)D片背景色 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ù)組存儲(chǔ)在object數(shù)組中。 參數(shù):strlength,代表需要產(chǎn)生的漢字個(gè)數(shù) */ public static object[] CreateCode(int strlength) { //定義一個(gè)字符串?dāng)?shù)組儲(chǔ)存漢字編碼的組成元素 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ù)組用來(lái) 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ǔ)產(chǎn)生的隨機(jī)漢字區(qū)位碼 byte byte1=Convert.ToByte(str_r1 + str_r2,16); byte byte2=Convert.ToByte(str_r3 + str_r4,16); //將兩個(gè)字節(jié)變量存儲(chǔ)在字節(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ò)誤,請(qǐng)重新輸入!", "提示", 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中使用@聲明變量示例(逐字標(biāo)識(shí)符)
這篇文章主要介紹了C#中使用@聲明變量示例(逐字標(biāo)識(shí)符)在C#中,@符號(hào)不僅可以加在字符串常量之前,使字符串不作轉(zhuǎn)義之用,還可以加在變量名之前,使變量名與關(guān)鍵字不沖突,這種用法稱為“逐字標(biāo)識(shí)符”,需要的朋友可以參考下2015-06-06C#通過yield實(shí)現(xiàn)數(shù)組全排列的方法
這篇文章主要介紹了C#通過yield實(shí)現(xiàn)數(shù)組全排列的方法,以實(shí)例形式較為詳細(xì)的分析了全排列的概念及C#的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03C# 如何使用OpcUaHelper讀寫OPC服務(wù)器
這篇文章給大家介紹C# 如何使用OpcUaHelper讀寫OPC服務(wù)器,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-12-12C#使用smtp發(fā)送帶附件的郵件實(shí)現(xiàn)方法
這篇文章主要介紹了C#使用smtp發(fā)送帶附件的郵件實(shí)現(xiàn)方法,可直接將string類型結(jié)果保存為附件,實(shí)例中備有相應(yīng)的注釋便于理解,需要的朋友可以參考下2014-11-11C#?WPF后臺(tái)動(dòng)態(tài)添加控件實(shí)戰(zhàn)教程
最近嘗試用wpf在后臺(tái)動(dòng)態(tài)添加控件,所以下面這篇文章主要給大家介紹了關(guān)于C#?WPF后臺(tái)動(dòng)態(tài)添加控件的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05C#實(shí)現(xiàn)百分比轉(zhuǎn)小數(shù)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)百分比轉(zhuǎn)小數(shù)的方法,涉及C#進(jìn)行數(shù)值計(jì)算的相關(guān)技巧,需要的朋友可以參考下2015-06-06利用C#/VB.NET實(shí)現(xiàn)將PDF轉(zhuǎn)為Word
眾所周知,PDF 文檔支持特長(zhǎng)文件,集成度和安全可靠性都較高,可有效防止他人對(duì) PDF 內(nèi)容進(jìn)行更改,所以在工作中深受大家喜愛。本文將分為兩部分介紹如何以編程的方式將 PDF 轉(zhuǎn)換為 Word,需要的可以參考一下2022-12-12基于Unity實(shí)現(xiàn)3D版2048游戲的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Unity實(shí)現(xiàn)簡(jiǎn)易的3D版2048游戲,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下2023-02-02