• C#验证码


    环境是.net 5开发。

    1.建立一个项目

    2.代码

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Drawing.Drawing2D;
    7. using System.Linq;
    8. using System.Text;
    9. using System.Windows.Forms;
    10. namespace WinFormsApp1
    11. {
    12. public partial class Form1 : Form
    13. {
    14. /// <summary>
    15. /// 验证码的值
    16. /// </summary>
    17. public string txt = string.Empty;
    18. public Form1()
    19. {
    20. InitializeComponent();
    21. }
    22. private void button1_Click(object sender, EventArgs e)
    23. {
    24. GetLettersNumbers();
    25. }
    26. private void button2_Click(object sender, EventArgs e)
    27. {
    28. GetChinese();
    29. }
    30. private void button3_Click(object sender, EventArgs e)
    31. {
    32. if (textBox1.Text.Trim() != txt)
    33. {
    34. MessageBox.Show("提示:验证码输入错误,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    35. }
    36. else
    37. {
    38. MessageBox.Show("提示:验证码输入正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    39. }
    40. }
    41. /// <summary>
    42. /// 字母和数字
    43. /// </summary>
    44. private void GetLettersNumbers()
    45. {
    46. //颜色列表,用于验证码、噪线、噪点
    47. Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
    48. //字体列表,用于验证码
    49. string[] font = { "Times New Roman" };
    50. //验证码的字符集,去掉了一些容易混淆的字符
    51. char[] character = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
    52. Random rnd = new Random();
    53. //生成验证码字符串
    54. txt = "";
    55. for (int i = 0; i < 4; i++)
    56. {
    57. txt += character[rnd.Next(character.Length)];
    58. }
    59. //创建画布
    60. int codeW = txt.Length * 22;
    61. int codeH = 22;
    62. Bitmap bmp = new Bitmap(codeW, codeH);
    63. Graphics g = Graphics.FromImage(bmp);
    64. g.Clear(Color.White);
    65. //画噪线
    66. for (int i = 0; i < 3; i++)
    67. {
    68. int x1 = rnd.Next(codeW);
    69. int y1 = rnd.Next(codeH);
    70. int x2 = rnd.Next(codeW);
    71. int y2 = rnd.Next(codeH);
    72. Color clr = color[rnd.Next(color.Length)];
    73. g.DrawLine(new Pen(clr), x1, y1, x2, y2);
    74. }
    75. //画验证码字符串
    76. for (int i = 0; i < txt.Length; i++)
    77. {
    78. string fnt = font[rnd.Next(font.Length)];
    79. Font ft = new Font(fnt, 12);
    80. Color clr = color[rnd.Next(color.Length)];
    81. g.DrawString(txt[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, (float)0);
    82. }
    83. pictureBox1.Image = bmp;
    84. }
    85. private void GetChinese()
    86. {
    87. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);//.net 5使用
    88. //获取GB2312编码页(表)
    89. Encoding gb = Encoding.GetEncoding("GB2312");
    90. //调用函数产生4个随机中文汉字编码
    91. object[] bytes = CreateCode(4);
    92. //根据汉字编码的字节数组解码出中文汉字
    93. string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
    94. string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
    95. string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
    96. string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
    97. txt = str1 + str2 + str3 + str4;
    98. if (txt == null || txt == String.Empty)
    99. {
    100. return;
    101. }
    102. Bitmap image = new Bitmap((int)Math.Ceiling((txt.Length * 20.5)), 22);
    103. Graphics g = Graphics.FromImage(image);
    104. try
    105. {
    106. //生成随机生成器
    107. Random random = new Random();
    108. //清空图片背景色
    109. g.Clear(Color.White);
    110. //画图片的背景噪音线
    111. for (int i = 0; i < 2; i++)
    112. {
    113. Point tem_Point_1 = new Point(random.Next(image.Width), random.Next(image.Height));
    114. Point tem_Point_2 = new Point(random.Next(image.Width), random.Next(image.Height));
    115. g.DrawLine(new Pen(Color.Black), tem_Point_1, tem_Point_2);
    116. }
    117. Font font = new Font("宋体", 12, (FontStyle.Bold));
    118. LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
    119. g.DrawString(txt, font, brush, 2, 2);
    120. //画图片的前景噪音点
    121. for (int i = 0; i < 100; i++)
    122. {
    123. Point tem_point = new Point(random.Next(image.Width), random.Next(image.Height));
    124. image.SetPixel(tem_point.X, tem_point.Y, Color.FromArgb(random.Next()));
    125. }
    126. //画图片的边框线
    127. g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
    128. pictureBox1.Image = image;
    129. }
    130. catch
    131. {
    132. MessageBox.Show("生成失败");
    133. }
    134. }
    135. public object[] CreateCode(int strlength)
    136. {
    137. //定义一个字符串数组储存汉字编码的组成元素
    138. string[] r = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
    139. Random rnd = new Random();
    140. //定义一个object数组用来
    141. object[] bytes = new object[strlength];
    142. for (int i = 0; i < strlength; i++)
    143. {
    144. //区位码第1位
    145. int r1 = rnd.Next(11, 14);
    146. string str_r1 = r[r1].Trim();
    147. //区位码第2位
    148. int r2;
    149. if (r1 == 13)
    150. r2 = rnd.Next(0, 7);
    151. else
    152. r2 = rnd.Next(0, 16);
    153. string str_r2 = r[r2].Trim();
    154. //区位码第3位
    155. int r3 = rnd.Next(10, 16);
    156. string str_r3 = r[r3].Trim();
    157. //区位码第4位
    158. int r4;
    159. if (r3 == 10)
    160. {
    161. r4 = rnd.Next(1, 16);
    162. }
    163. else if (r3 == 15)
    164. {
    165. r4 = rnd.Next(0, 15);
    166. }
    167. else
    168. {
    169. r4 = rnd.Next(0, 16);
    170. }
    171. string str_r4 = r[r4].Trim();
    172. //定义两个字节变量存储产生的随机汉字区位码
    173. byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
    174. byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
    175. //将两个字节变量存储在字节数组中
    176. byte[] str_r = new byte[] { byte1, byte2 };
    177. //将产生的一个汉字的字节数组放入object数组中
    178. bytes.SetValue(str_r, i);
    179. }
    180. return bytes;
    181. }
    182. }
    183. }

    3.效果

     

    注意:

    如果是.net 5的项目,nuget中增加,System.Text.Encoding.CodePages 

  • 相关阅读:
    光模块在5G网络中的使用
    Python获取Pandas列名的几种方法(含代码示例)
    DS2431P+T&R 一款可以独立的存储区页,1024位EEPROM存储器芯片
    Node-RED系列教程-22node-red操作modbusRTU
    Tomcat
    设计模式——职责链模式
    LeetCode Top100:两数之和-java版
    数据库连接池有什么用?它有哪些关键参数?
    亚洲国家列表 Asia country list
    工厂设计模式
  • 原文地址:https://blog.csdn.net/u012563853/article/details/125505312