您的足迹:首页 > 未分类 >自己写了个C# 生成简单验证码和加减算法验证码

自己写了个C# 生成简单验证码和加减算法验证码

自己写了个C# 生成简单验证码和加减算法验证码 

思路:

1.随机生成数字或字母,如果是加减验证码的话生成数字和 + - 

2.绘制到Image上

3.随机生成位置,在上面绘制大小为1,1的噪点,绘制多少看具体效果。一般100~200左右。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Controller
{
    /// <summary>
    /// 验证码帮助类
    /// </summary>
    public class CheckCodeHelper
    {
        /// <summary>
        /// 验证码长度
        /// </summary>
        public int CodeWidth;

        /// <summary>
        /// 验证码高度
        /// </summary>
        public int CodeHeight;

        /// <summary>
        /// 验证码结果值
        /// </summary>
        public string ReturnValue;

        public CheckCodeHelper(int width, int height)
        {
            CodeWidth = width;
            CodeHeight = height;
            ReturnValue = null;
        }

        /// <summary>
        /// 生成随机数字
        /// </summary>
        /// <returns></returns>
        private string GenerateNumbers(int count)
        {
            string strOut = "";
            Random random = new Random();
            for (int i = 0; i < count; i++)
            {
                string num = Convert.ToString(random.Next(10000) % 10);
                strOut += num;
            }
            return strOut.Trim();
        }

        /// <summary>
        /// 生成随机字符
        /// </summary>
        /// <returns></returns>
        private string GenerateCharacters(int count)
        {
            string strOut = "";
            Random random = new Random();
            for (int i = 0; i < count; i++)
            {
                string num = Convert.ToString((char)(65 + random.Next(10000) % 26));
                strOut += num;
            }
            return strOut.Trim();
        }

        private string GetNumAndStrCharacters(int count)
        {
            string outStr = "";

            string nums = GenerateNumbers(count);
            string str = GenerateCharacters(count);

            Random random = new Random();
            string value = nums + str;
            for (int i = 0; i < count; i++)
            {
                outStr += value.Substring(random.Next(0, value.Length), 1);
            }
            ReturnValue = outStr;
            return outStr;
        }

        /// <summary>
        /// 获取加减计算验证码
        /// </summary>
        /// <returns></returns>
        private string GetMathNumbers()
        {
            string strOut = "";
            int ValueOut = 0;
            Random random = new Random();
            int number = random.Next(0, 30);
            int number2 = random.Next(0, 30);
            int DectX = random.Next(0, 100) % 2;
            if (DectX == 0)
            {
                strOut = number + " + " + number2;
                ValueOut = number + number2;
            }
            else
            {
                if (number >= number2)
                {
                    strOut = number + " - " + number2;
                    ValueOut = number - number2;
                }
                else
                {
                    strOut = number2 + " - " + number;
                    ValueOut = number2 - number;
                }
            }
            ReturnValue = ValueOut.ToString();
            return strOut;
        }

        /// <summary>
        /// 创建加减计算的验证码
        /// </summary>
        /// <returns></returns>
        public Image CreateMathImage()
        {
            Bitmap bmp = new Bitmap(CodeWidth, CodeHeight);

            Graphics graph = Graphics.FromImage(bmp);
            Color[] penColors = new Color[] { Color.Black, Color.Red, Color.Blue, Color.Green, Color.HotPink };
            Color[] colors = new Color[] { Color.Black, Color.Red, Color.Blue, Color.Green, Color.HotPink, Color.Yellow };
            FontStyle[] fontStyles = new FontStyle[] { FontStyle.Regular, FontStyle.Bold, FontStyle.Italic };

            Random random = new Random();
            string number = GetMathNumbers();

            for (int i = 0; i < number.Length; i++)
            {
                Font font = new Font("宋体", random.Next(12, 16), fontStyles[random.Next(0, fontStyles.Length)]);
                Color color = penColors[random.Next(0, penColors.Length)];
                graph.DrawString(number.Substring(i, 1), font, new SolidBrush(color), (i * 10), 5);

            }
            //加噪点
            for (int i = 0; i < 200; i++)
            {
                Color color = colors[random.Next(0, colors.Length)];
                graph.FillRectangle(new SolidBrush(color), random.Next(0, CodeWidth), random.Next(0, CodeHeight), 1, 1);
            }
            graph.Dispose();
            return bmp;
        }

        /// <summary>
        /// 创建字符和数字的验证码
        /// </summary>
        /// <returns></returns>
        public Image CreateNumAndStrImage()
        {
            Bitmap bmp = new Bitmap(CodeWidth, CodeHeight);

            Graphics graph = Graphics.FromImage(bmp);
            Color[] penColors = new Color[] { Color.Black, Color.Red, Color.Blue, Color.Green, Color.HotPink };
            Color[] colors = new Color[] { Color.Black, Color.Red, Color.Blue, Color.Green, Color.HotPink, Color.Yellow };
            FontStyle[] fontStyles = new FontStyle[] { FontStyle.Regular, FontStyle.Bold, FontStyle.Italic };

            Random random = new Random();
            string number = GetNumAndStrCharacters(4);

            for (int i = 0; i < number.Length; i++)
            {
                Font font = new Font("宋体", random.Next(14, 20), fontStyles[random.Next(0, fontStyles.Length)]);
                Color color = penColors[random.Next(0, penColors.Length)];
                graph.DrawString(number.Substring(i, 1), font, new SolidBrush(color), (i * 18), 5);

            }
            //加噪点
            for (int i = 0; i < 200; i++)
            {
                Color color = colors[random.Next(0, colors.Length)];
                graph.FillRectangle(new SolidBrush(color), random.Next(0, CodeWidth), random.Next(0, CodeHeight), 1, 1);
            }
            graph.Dispose();
            return bmp;
        }
    }
}

本博客所有文章如无特别注明均为原创。作者:cc复制或转载请以超链接形式注明转自 我的博客
原文地址《自己写了个C# 生成简单验证码和加减算法验证码

相关推荐

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)