最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)證碼輸入錯誤,請重新輸入!", "提示", 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)文章

  • C#實(shí)現(xiàn)UDP通信方式

    C#實(shí)現(xiàn)UDP通信方式

    文章介紹了如何使用C#實(shí)現(xiàn)UDP通信,包括UDP服務(wù)器和客戶端的實(shí)現(xiàn)步驟和示例代碼,服務(wù)器關(guān)鍵類為UdpClient和IPEndPoint,實(shí)例化對象后可以通過異步任務(wù)發(fā)送數(shù)據(jù)并接收數(shù)據(jù),客戶端同樣使用UdpClient和IPEndPoint,連接到遠(yuǎn)程服務(wù)器后開新任務(wù)接收數(shù)據(jù)
    2025-01-01
  • 在C#中如何獲取程序集

    在C#中如何獲取程序集

    某一天我正在寫一些反射代碼,目的是遍歷所有的程序集來查找一個(gè)特定的接口,然后在Startup中調(diào)用其上的一個(gè)方法,這篇文章主要介紹了在C#中如何獲取程序集,需要的朋友可以參考下
    2024-03-03
  • Unity?UGUI的PointerEventData的介紹及使用

    Unity?UGUI的PointerEventData的介紹及使用

    這篇文章主要為大家介紹了Unity?UGUI的PointerEventData的介紹及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 關(guān)于C#反射 你需要知道的

    關(guān)于C#反射 你需要知道的

    這篇文章主要介紹了C#反射的相關(guān)知識,文中講解的非常詳細(xì),代碼幫助大家更好的參考學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 詳解C# Protobuf如何做到0分配內(nèi)存的序列化

    詳解C# Protobuf如何做到0分配內(nèi)存的序列化

    這篇文章主要介紹了詳解C# Protobuf如何做到0分配內(nèi)存的序列化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • .Net中的json操作類用法分析

    .Net中的json操作類用法分析

    這篇文章主要介紹了.Net中的json操作類用法分析,是非常實(shí)用的一個(gè)技巧,需要的朋友可以參考下
    2014-08-08
  • C#之HttpClient的同步使用方式

    C#之HttpClient的同步使用方式

    這篇文章主要介紹了C#之HttpClient的同步使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • C#實(shí)現(xiàn)DataTable轉(zhuǎn)換成IList的方法

    C#實(shí)現(xiàn)DataTable轉(zhuǎn)換成IList的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)DataTable轉(zhuǎn)換成IList的方法,涉及C#針對DataTable操作的相關(guān)技巧,需要的朋友可以參考下
    2016-03-03
  • 淺聊一下C#中內(nèi)存映射文件的玩法

    淺聊一下C#中內(nèi)存映射文件的玩法

    內(nèi)存映射文件是怎么玩的,說實(shí)話這東西理論我相信很多朋友都知道,就是將文件映射到進(jìn)程的虛擬地址,說起來很容易,那如何讓大家眼見為實(shí)呢,本文就來和大家簡單聊聊
    2023-06-06
  • C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘二 線性結(jié)構(gòu)

    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

最新評論

五河县| 新源县| 盐山县| 西贡区| 冕宁县| 中西区| 舒城县| 会泽县| 梁河县| 通道| 客服| 英吉沙县| 孝昌县| 定结县| 阳东县| 霍山县| 伊宁县| 茂名市| 内黄县| 宁晋县| 沾益县| 东乡| 新津县| 武乡县| 沂南县| 于田县| 广灵县| 泰顺县| 大理市| 长治市| 海城市| 五河县| 五莲县| 邓州市| 于田县| 巩留县| 宜兰市| 突泉县| 怀柔区| 特克斯县| 曲麻莱县|