当前位置:   article > 正文

生成验证码_pb 生成公式验证码

pb 生成公式验证码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Text;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace CAPTCHA
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. /// <summary>
  20. /// 实现点一下就换一张验证码
  21. /// </summary>
  22. /// <param name="sender"></param>
  23. /// <param name="e"></param>
  24. private void pbCaptcha_Click(object sender, EventArgs e)
  25. {
  26. Bitmap bmp = new Bitmap(120, 40);
  27. Graphics g = Graphics.FromImage(bmp);
  28. Random r = new Random();
  29. string str = "";
  30. for (int i = 0; i < 5; i++)
  31. {
  32. int rNum= r.Next(0, 10);
  33. str += rNum;
  34. }
  35. string[] fonts ={"华文行楷","微软雅黑","楷体","仿宋","宋体"};
  36. Color[] colors = { Color.Red,Color.RosyBrown,Color.DarkOrange,Color.SteelBlue,Color.DarkTurquoise};
  37. //随机字体
  38. InstalledFontCollection fc = new InstalledFontCollection();
  39. FontFamily[] fs = fc.Families;
  40. for (int i = 0; i < 5; i++)
  41. {
  42. Point p = new Point(i * 20, 0);
  43. g.DrawString(str[i].ToString(), new Font(fs[r.Next(0,fs.Length)],20,FontStyle.Bold), new SolidBrush(colors[i]), p);
  44. }
  45. //画50条线
  46. for (int i = 0; i < 40; i++)
  47. {
  48. Point p1 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
  49. Point p2 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
  50. g.DrawLine(new Pen(Color.LightGray),p1, p2);
  51. }
  52. //画50个点
  53. for (int i = 0; i < 50; i++)
  54. {
  55. Point p = new Point(r.Next(0, bmp.Height), r.Next(0, bmp.Width));
  56. bmp.SetPixel( p.Y,p.X, Color.Gray);
  57. }
  58. pbCaptcha.Image = bmp;
  59. }
  60. }
  61. }

效果:



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/742014
推荐阅读
相关标签
  

闽ICP备14008679号