C#實現(xiàn)中文驗證碼的示例代碼
更新時間:2022年12月12日 10:52:19 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實現(xiàn)中文驗證碼功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下
實踐過程
效果
代碼
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個隨機中文漢字編碼 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 { //生成隨機生成器 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); //畫圖片的前景噪音點 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)隨機創(chuàng)建含兩個元素的十六進(jìn)制字節(jié)數(shù)組,每個字節(jié)數(shù)組代表一個漢字,并將 四個字節(jié)數(shù)組存儲在object數(shù)組中。 參數(shù):strlength,代表需要產(chǎn)生的漢字個數(shù) */ public static object[] CreateCode(int strlength) { //定義一個字符串?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(); //定義一個object數(shù)組用來 object[] bytes=new object[strlength]; /**//*每循環(huán)一次產(chǎn)生一個含兩個元素的十六進(jìn)制字節(jié)數(shù)組,并將其放入bject數(shù)組中 每個漢字有四個區(qū)位碼組成 區(qū)位碼第1位和區(qū)位碼第2位作為字節(jié)數(shù)組第一個元素 區(qū)位碼第3位和區(qū)位碼第4位作為字節(jié)數(shù)組第二個元素 */ 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);//更換隨機數(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(); //定義兩個字節(jié)變量存儲產(chǎn)生的隨機漢字區(qū)位碼 byte byte1=Convert.ToByte(str_r1 + str_r2,16); byte byte2=Convert.ToByte(str_r3 + str_r4,16); //將兩個字節(jié)變量存儲在字節(jié)數(shù)組中 byte[] str_r=new byte[]{byte1,byte2}; //將產(chǎn)生的一個漢字的字節(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("提示:驗證碼輸入正確!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } else { MessageBox.Show("提示:驗證碼輸入錯誤,請重新輸入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }
partial class Form1 { /// <summary> /// 必需的設(shè)計器變量。 /// </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è)計器生成的代碼 /// <summary> /// 設(shè)計器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內(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 = "輸入驗證碼:"; // // 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 = "中文驗證碼"; 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#實現(xiàn)中文驗證碼的示例代碼的文章就介紹到這了,更多相關(guān)C# 中文驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#通過yield實現(xiàn)數(shù)組全排列的方法
這篇文章主要介紹了C#通過yield實現(xiàn)數(shù)組全排列的方法,以實例形式較為詳細(xì)的分析了全排列的概念及C#的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03C# 如何使用OpcUaHelper讀寫OPC服務(wù)器
這篇文章給大家介紹C# 如何使用OpcUaHelper讀寫OPC服務(wù)器,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-12-12C#使用smtp發(fā)送帶附件的郵件實現(xiàn)方法
這篇文章主要介紹了C#使用smtp發(fā)送帶附件的郵件實現(xiàn)方法,可直接將string類型結(jié)果保存為附件,實例中備有相應(yīng)的注釋便于理解,需要的朋友可以參考下2014-11-11C#實現(xiàn)百分比轉(zhuǎn)小數(shù)的方法
這篇文章主要介紹了C#實現(xiàn)百分比轉(zhuǎn)小數(shù)的方法,涉及C#進(jìn)行數(shù)值計算的相關(guān)技巧,需要的朋友可以參考下2015-06-06利用C#/VB.NET實現(xiàn)將PDF轉(zhuǎn)為Word
眾所周知,PDF 文檔支持特長文件,集成度和安全可靠性都較高,可有效防止他人對 PDF 內(nèi)容進(jìn)行更改,所以在工作中深受大家喜愛。本文將分為兩部分介紹如何以編程的方式將 PDF 轉(zhuǎn)換為 Word,需要的可以參考一下2022-12-12