image.png

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. namespace _127_绘制验证码
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public Form1()
    15. {
    16. InitializeComponent();
    17. }
    18. //用来记录验证码中的数字
    19. string correct = null;
    20. /// <summary>
    21. /// 点击PictureBox时验证码更换
    22. /// </summary>
    23. /// <param name="sender"></param>
    24. /// <param name="e"></param>
    25. private void pictureBoxCode_Click(object sender, EventArgs e)
    26. {
    27. Random r = new Random();
    28. string strCode = null;
    29. for (int i = 0; i < 5; i++)
    30. {
    31. int rNumber = r.Next(0, 10);
    32. strCode += rNumber;
    33. }
    34. //将正确数字记录下来
    35. correct = strCode;
    36. //创建随机数、干扰线和像素点的载体
    37. //创建GDI对象 (画图像)
    38. Bitmap bmp = new Bitmap(200, 50); //创建一个位图对象
    39. Graphics g = Graphics.FromImage(bmp);
    40. //将随机数画入bmp中
    41. for (int i = 0; i < 5; i++)
    42. {
    43. //字体,颜色,样式要求全随机
    44. string[] fonts = { "宋体", "微软雅黑", "幼圆", "华文行楷", "方正舒体", "华文彩云", "楷体" };
    45. Color[] colors = { Color.Yellow, Color.Red, Color.Green, Color.Blue, Color.Black, Color.Violet, Color.Orange, Color.Azure };
    46. FontStyle[] fStyle = { FontStyle.Regular, FontStyle.Bold, FontStyle.Italic, FontStyle.Underline, FontStyle.Strikeout };
    47. //五个数字的横坐标不能相同,纵坐标可以
    48. Point point = new Point(i * 30, 0);//在PictureBox中的坐标
    49. //画string DrawString(str,font,brush,point)
    50. //DrawString()方法需要 string font brush point四个参数 (Font需要字体,大小,类型三个参数) //定义一种颜色的画笔,这个方法可以将color传入
    51. g.DrawString(strCode[i].ToString(), new Font(fonts[r.Next(0, fonts.Length)], r.Next(20, 55), fStyle[r.Next(0, fStyle.Length)]), new SolidBrush(colors[r.Next(0, colors.Length)]), point);
    52. }
    53. //画干扰线
    54. for (int i = 0; i < 70; i++)
    55. {
    56. Color[] colors = { Color.Yellow, Color.Red, Color.Green, Color.Blue, Color.Black, Color.Violet, Color.Orange, Color.Azure };
    57. Pen pen = new Pen(colors[r.Next(0, colors.Length)]);
    58. Point p1 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
    59. Point p2 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
    60. g.DrawLine(pen, p1, p2);
    61. }
    62. //生成像素粒
    63. for (int i = 0; i < 500; i++)
    64. {
    65. Point p = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
    66. Color[] colors = { Color.Yellow, Color.Red, Color.Green, Color.Blue, Color.Black, Color.Violet, Color.Orange, Color.Azure };
    67. //在此Bitmap中设置指定像素的颜色
    68. bmp.SetPixel(p.X, p.Y, colors[r.Next(0, colors.Length)]);
    69. }
    70. //将图片镶嵌到PictureBox中
    71. pictureBoxCode.Image = bmp;
    72. }
    73. /// <summary>
    74. /// 点击时,判断验证码是否与用户输入的相同
    75. /// </summary>
    76. /// <param name="sender"></param>
    77. /// <param name="e"></param>
    78. private void btnEnter_Click(object sender, EventArgs e)
    79. {
    80. if (txtInput.Text == correct)
    81. {
    82. MessageBox.Show("输入正确");
    83. Object ob = new object();
    84. EventArgs ea = new EventArgs();
    85. pictureBoxCode_Click(ob, ea);
    86. }
    87. else
    88. {
    89. MessageBox.Show("输入错误,沙币");
    90. }
    91. //输入后清空文本
    92. txtInput.Clear();
    93. txtInput.Focus();
    94. }
    95. private void label1_Click(object sender, EventArgs e)
    96. {
    97. Object ob = new object();
    98. EventArgs ea = new EventArgs();
    99. pictureBoxCode_Click(ob, ea);
    100. }
    101. private void Form1_Load(object sender, EventArgs e)
    102. {
    103. Object ob = new object();
    104. EventArgs ea = new EventArgs();
    105. pictureBoxCode_Click(ob, ea);
    106. }
    107. }
    108. }