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

using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Text;
using System;
namespace CustomEx
{
    public class MyPic
    {
        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 MyPic(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)
        {
            Calculate ca = null;
            switch (fuhao)
            {
                case "+": ca = new AddCalculate(); break;
                case "-": ca = new ReduceCalculate(); break;
            }
            return ca.Calc(frist, scoend);
        }
    }
}

         

 

转载于:https://www.cnblogs.com/student-note/p/6496550.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值