• C# 验证码(字母验证,公式验证)


    
    
        public class VerificationCodeHelper
        {
            private int isFuhao = 0;
            private List<string> _fuHao;
            private List<Color> _Color;
            private List<Font> _Font;
            private int x = 0;
            private int y = 0;
            public VerificationCodeHelper(int width, int height)
            {
                this.width = width;
                this.height = height;
                this._fuHao = new List<string>() {
                "+",
                "-"
                };
                this._Color = new List<Color>() {Color.Red,
                    Color.Black,
                    Color.Blue,
                    Color.Green,
                     System.Drawing.ColorTranslator.FromHtml("#FF00FF"),
                     System.Drawing.ColorTranslator.FromHtml("#CD0000"),
                     System.Drawing.ColorTranslator.FromHtml("#912CEE"),
                     System.Drawing.ColorTranslator.FromHtml("#00F5FF"),
                     System.Drawing.ColorTranslator.FromHtml("#00FF00")
                };
                this._Font = new List<Font>() {
                new Font("宋体",width/6,FontStyle.Bold),
                new Font("楷体",width/6,FontStyle.Italic),
                new Font("新宋体",width/6,FontStyle.Regular)
                };
    
            }
            private Bitmap GetMap(int Width, int Height)
            {
                Bitmap b = new Bitmap(Width, Height);
                return b;
            }
            private int width;
            private int height;
            public Tuple<Bitmap, string> DrawZimu()
            {
                StringBuilder sb = new StringBuilder();
                Bitmap b = GetMap(width, height);
                //生成画布
                Graphics g = Graphics.FromImage(b);
                //画背景图
                g.FillRectangle(Brushes.White, 0, 0, width, height);
                //画字
                for (int i = 0; i < 4; i++)
                {
                    string temp = RandomString();
                    sb.Append(temp);
                    DrawString(g, temp);
                }
                //画干扰线
                DrawGanRao(g, 40);
                //画干扰点
                DrawGanRaoDiao(b, 1000);
                g.Dispose();
                return new Tuple<Bitmap, string>(b, sb.ToString());
            }
            private void DrawString(Graphics g, string s)
            {
                y = RandomToInt(height - height / 2);
                g.DrawString(s,
    _Font[RandomToInt(_Font.Count)],
    new SolidBrush(_Color[RandomToInt(_Color.Count)]),
    new PointF(x, y)
    );
                g.RotateTransform(RandomToInt(6));
                x += width / 4;
            }
            private void DrawGanRao(Graphics g, int j)
            {
                for (int i = 0; i < j; i++)
                {
                    g.DrawLine(new Pen(_Color[RandomToInt(_Color.Count)]),
    RandomToInt(width),
    RandomToInt(height),
    RandomToInt(width),
    RandomToInt(height)
    );
                }
    
            }
            private void DrawGanRaoDiao(Bitmap b, int j)
            {
                for (int i = 0; i < j; i++)
                {
                    b.SetPixel(RandomToInt(width),
            RandomToInt(height),
            _Color[RandomToInt(_Color.Count)]);
                }
    
            }
            public Tuple<Bitmap, int> DrawGongs()
            {
                Bitmap b = GetMap(this.width, this.height);
                Graphics g = Graphics.FromImage(b);
                g.FillRectangle(Brushes.White, 0, 0, width, height);
                string temp = string.Empty;
                int frist = GetString();
                int scoend = GetString();
                string fuhao = this._fuHao[RandomToInt(_fuHao.Count)];
                DrawString(g, frist.ToString());
                DrawString(g, fuhao);
                DrawString(g, scoend.ToString());
                DrawString(g, "=");
                g.Dispose();
                return new Tuple<Bitmap, int>(b, GetResult(fuhao, frist, scoend));
            }
            /// <summary>
            /// 随机得到数字
            /// </summary>
            /// <returns></returns>
            private int GetString()
            {
                return RandomToInt(50);
            }
            Random r = new Random();
            /// <summary>
            /// 随机字符
            /// </summary>
            /// <returns></returns>
            private string RandomString()
            {
                string s = ((char)r.Next(65, 90)).ToString();
                return s;
            }
            private int RandomToInt(int i)
            {
                return r.Next(0, i);
            }
            private int GetResult(string fuhao, int frist, int scoend)
            {
                ICalculate ca = null;
                switch (fuhao)
                {
                    case "+": ca = new AddCalculate(); break;
                    case "-": ca = new ReduceCalculate(); break;
                }
                return ca.Calc(frist, scoend);
            }
        }
        public interface ICalculate
        {
            public int Calc(int first, int scoend);
        }
        public class AddCalculate : ICalculate
        {
            public int Calc(int first, int scoend)
            {
                return first + scoend;
            }
        }
        public class ReduceCalculate : ICalculate
        {
            public int Calc(int first, int scoend)
            {
                return first - scoend;
            }
        }
     

             

    Hold on, everything is possible.
  • 相关阅读:
    篝火晚会
    SECHS
    emmc4.X boot1 and boot2
    imx6Q Android7.1 Udisk Mount
    imx6Q 4.1.15 Perf support
    imx6Q 4.1.15 Kgtp support
    imx6Q 4.1.15 early console support
    imx6Q USB OTG Host/Device纯软件切换
    iMX6Q DDR Stresstest
    iMX6Q PowerSave调试
  • 原文地址:https://www.cnblogs.com/student-note/p/6496550.html
Copyright © 2020-2023  润新知