欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# WinForm 登錄界面的圖片驗證碼(區(qū)分大小寫+不區(qū)分大小寫)

 更新時間:2020年02月17日 11:16:57   作者:人生、蛻變  
這篇文章主要介紹了C# WinForm 登錄界面的圖片驗證碼(區(qū)分大小寫+不區(qū)分大小寫),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、功能界面

圖1 驗證碼(區(qū)分大小寫)

圖2 驗證碼(不區(qū)分大小寫)

二、創(chuàng)建一個產(chǎn)生驗證碼的類Class1

(1)生成隨機驗證碼字符串,用的是Random隨機函數(shù)
(2)創(chuàng)建驗證碼圖片,將該字符串畫在PictureBox控件中

Class1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;//圖片
using System.Windows.Forms;

namespace ValidCodeTest
{
  public class Class1
  {
    #region 驗證碼功能      
    /// <summary>
    /// 生成隨機驗證碼字符串
    /// </summary>
    public static string CreateRandomCode(int CodeLength) 
    {
      int rand;
      char code;
      string randomCode = String.Empty;//隨機驗證碼
     	
     	//生成一定長度的隨機驗證碼    
      //Random random = new Random();//生成隨機數(shù)對象
      for (int i = 0; i < CodeLength; i++)
      {
        //利用GUID生成6位隨機數(shù)   
        byte[] buffer = Guid.NewGuid().ToByteArray();//生成字節(jié)數(shù)組
        int seed = BitConverter.ToInt32(buffer, 0);//利用BitConvert方法把字節(jié)數(shù)組轉(zhuǎn)換為整數(shù)
        Random random = new Random(seed);//以生成的整數(shù)作為隨機種子
        rand = random.Next();

        //rand = random.Next();   
        if (rand % 3 == 1)
        {
          code = (char)('A' + (char)(rand % 26));
        }
        else if (rand % 3 == 2)
        {
          code = (char)('a' + (char)(rand % 26));          
        }
        else
        {
          code = (char)('0' + (char)(rand % 10));
        }
        randomCode += code.ToString();
      }
      return randomCode;
    }

    /// <summary>
    /// 創(chuàng)建驗證碼圖片
    /// </summary>
    public static void CreateImage(string strValidCode, PictureBox pbox)
    {
      try
      {
        int RandAngle = 45;//隨機轉(zhuǎn)動角度
        int MapWidth = (int)(strValidCode.Length * 21);
        Bitmap map = new Bitmap(MapWidth, 28);//驗證碼圖片—長和寬

				//創(chuàng)建繪圖對象Graphics
        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),防止機器識別
        char[] chars = strValidCode.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(-RandAngle, RandAngle);//轉(zhuǎn)動的度數(shù)
          graph.TranslateTransform(dot.X, dot.Y);//移動光標到指定位置
          graph.RotateTransform(angle);
          graph.DrawString(chars[i].ToString(), f, b, 1, 1, format);

          graph.RotateTransform(-angle);//轉(zhuǎn)回去
          graph.TranslateTransform(2, -dot.Y);//移動光標到指定位置
        }
        pbox.Image = map;
      }
      catch (ArgumentException)
      {
        MessageBox.Show("驗證碼圖片創(chuàng)建錯誤");
      }
    }
    #endregion
  }
}

三、調(diào)用

(1)更新驗證碼
(2)驗證(區(qū)分大小寫)
(3)驗證(不區(qū)分大小寫)

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ValidCodeTest;

namespace ValidCode
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }


    #region 驗證碼
    private const int ValidCodeLength = 4;//驗證碼長度    
    private String strValidCode = "";//驗證碼            

    //調(diào)用自定義函數(shù),更新驗證碼
    private void UpdateValidCode()
    {
      strValidCode = Class1.CreateRandomCode(ValidCodeLength);//生成隨機驗證碼
      if (strValidCode == "") return;
      Class1.CreateImage(strValidCode, pbox1);//創(chuàng)建驗證碼圖片
    }
    #endregion


    private void pbox1_Click(object sender, EventArgs e)
    {
      UpdateValidCode();//點擊更新驗證碼
    }


    private void Form1_Load(object sender, EventArgs e)
    {
      UpdateValidCode();//加載更新驗證碼
    }


    /// <summary>
    /// 驗證(區(qū)分大小寫)
    /// </summary>    
    private void btn1_Click(object sender, EventArgs e)
    {
      string validcode = txtValidCode.Text.Trim();

      char[] ch1 = validcode.ToCharArray();
      char[] ch2 = strValidCode.ToCharArray();
      int Count1 = 0;//字母個數(shù)
      int Count2 = 0;//數(shù)字個數(shù)

      if (String.IsNullOrEmpty(validcode) != true)//驗證碼不為空
      {
        for (int i = 0; i < strValidCode.Length; i++)
        {
          if ((ch1[i] >= 'a' && ch1[i] <= 'z') || (ch1[i] >= 'A' && ch1[i] <= 'Z'))//字母
          {
            if (ch1[i] == ch2[i])
            {
              Count1++;
            }
          }
          else//數(shù)字
          {
            if (ch1[i] == ch2[i])
            {
              Count2++;
            }
          }

        }

        int CountSum = Count1 + Count2;
        if (CountSum == strValidCode.Length)
        {
          MessageBox.Show("驗證通過", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
          UpdateValidCode();
          txtValidCode.Text = "";
          txtValidCode.Focus();
        }
        else
        {
          MessageBox.Show("驗證失敗", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          UpdateValidCode();//更新驗證碼
          txtValidCode.Text = "";
          txtValidCode.Focus();
        }
      }
      else//驗證碼為空
      {
        MessageBox.Show("請輸入驗證碼", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        UpdateValidCode();//更新驗證碼
        txtValidCode.Text = "";
        txtValidCode.Focus();
      }
    }


    /// <summary>
    /// 驗證(不區(qū)分大小寫)
    /// </summary> 
    private void btn2_Click(object sender, EventArgs e)
    {
      string validcode = txtValidCode.Text.Trim();

      if (String.IsNullOrEmpty(validcode) != true)//驗證碼不為空
      {
        if (validcode.ToLower() == strValidCode.ToLower())
        {
          MessageBox.Show("驗證通過", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
          UpdateValidCode();
          txtValidCode.Text = "";
          txtValidCode.Focus();
        }
        else
        {
          MessageBox.Show("驗證失敗", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          UpdateValidCode();//更新驗證碼
          txtValidCode.Text = "";
          txtValidCode.Focus();
        }
      }
      else//驗證碼為空
      {
        MessageBox.Show("請輸入驗證碼", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        UpdateValidCode();//更新驗證碼
        txtValidCode.Text = "";
        txtValidCode.Focus();
      }
    }
  }
}

.exe測試文件下載: ValidCode_jb51.zip

參考文章:
https://www.jianshu.com/p/d89f22cf51bf

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Unity3D實現(xiàn)人物轉(zhuǎn)向與移動

    Unity3D實現(xiàn)人物轉(zhuǎn)向與移動

    這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)人物轉(zhuǎn)向與移動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • C#使用AutoUpdater.NET實現(xiàn)程序自動更新

    C#使用AutoUpdater.NET實現(xiàn)程序自動更新

    開發(fā)桌面應用程序的時候,經(jīng)常會因為新增功能需求或修復已知問題,要求客戶更新應用程序,在.Net體系中采用?AutoUpdater.NET?組件可以非常便捷的實現(xiàn)這一功能,需要的朋友可以參考下
    2024-02-02
  • 詳解C#如何使用消息隊列MSMQ

    詳解C#如何使用消息隊列MSMQ

    消息隊列 (MSMQ Microsoft Message Queuing)是MS提供的服務,也就是Windows操作系統(tǒng)的功能,下面就跟隨小編一起了解一下C#中是如何使用消息隊列MSMQ的吧
    2024-01-01
  • c# 獲取已安裝的打印機并調(diào)用打印文件

    c# 獲取已安裝的打印機并調(diào)用打印文件

    這篇文章主要介紹了c#如何獲取已安裝的打印機并調(diào)用打印文件,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-10-10
  • C#畫筆使用復合數(shù)組繪制單個矩形的方法

    C#畫筆使用復合數(shù)組繪制單個矩形的方法

    這篇文章主要介紹了C#畫筆使用復合數(shù)組繪制單個矩形的方法,涉及C#使用畫筆繪制圖形的相關技巧,需要的朋友可以參考下
    2015-06-06
  • 關于C#基礎知識回顧--反射(一)

    關于C#基礎知識回顧--反射(一)

    其實說白了,反射就是能知道我們未知類型的類型信息這么一個東西.沒什么神秘可講!反射的核心是System.Type。System.Type包含了很多屬性和方法,使用這些屬性和方法可以在運行時得到類型信息
    2013-07-07
  • Netcore?Webapi返回數(shù)據(jù)的三種方式示例

    Netcore?Webapi返回數(shù)據(jù)的三種方式示例

    這篇文章主要為大家介紹了Netcore?Webapi返回數(shù)據(jù)的三種方式示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • C#圖片按比例縮放實例

    C#圖片按比例縮放實例

    這篇文章主要為大家詳細介紹了C#圖片按比例縮放的實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 淺談C#中HttpWebRequest與HttpWebResponse的使用方法

    淺談C#中HttpWebRequest與HttpWebResponse的使用方法

    本篇文章主要介紹了淺談C#中HttpWebRequest與HttpWebResponse的使用方法,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01
  • C#自定義事件之屬性改變引發(fā)事件示例

    C#自定義事件之屬性改變引發(fā)事件示例

    這篇文章主要為大家詳細介紹了C#自定義事件之屬性改變引發(fā)事件示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評論