C#生成随机验证码

2021-02-08 05:16

阅读:608

标签:encoding   foreach   ref   ppa   return   字体   als   inf   die   

技术图片  技术图片  技术图片  技术图片  技术图片

可以生成验证码类型有“字母字符串” 、“常用的汉字”、“简单的数学运算”。生成的验证码图片有jpeg和gif两种格式。

生成随机验证码包括数字字母和汉字两种形式,汉字形式利用GB2312编码表生成而且是常用的汉字。

GB2312 字符编码分布表:

分区范围
符号类型
第01区
中文标点、数学符号以及一些特殊字符
第02区
各种各样的数学序号
第03区
全角西文字符
第04区
日文平假名
第05区
日文片假名
第06区
希腊字母表
第07区
俄文字母表
第08区
中文拼音字母表
第09区
制表符号
第10-15区
无字符
第16-55区
一级汉字(以拼音字母排序)
第56-87区
二级汉字(以部首笔画排序)
第88-94区
无字符
 public class VerificationCode
  {

    //产生验证码的字符集
    private static string[] ValidateCharArray = new string[] { "2", "3", "4", "5", "6", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "R", "S", "U", "W", "X", "Y", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "r", "s", "u", "w", "x", "y" };
    //产生验证码的算术符号
    private static string[] ArithmeticSymbol = new string[] { "", "", "×", "÷" };
    //64位图片字符串开头描述
    private const string imageStr = "data:image/{0};base64,";

    ///
    ///生成随机验证码(数字字母)
    ///
    ///验证码的长度
    ///
    public static string CreateValidateNumber(int length)
    {
      string randomCode = "";
      int temp = -1;

      Random rand = new Random();
      for (int i = 0; i )
      {
        if (temp != -1)
        {
          rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
        }
        int t = rand.Next(ValidateCharArray.Length);
        if (temp != -1 && temp == t)
        {
          return CreateValidateNumber(length);
        }
        temp = t;
        randomCode += ValidateCharArray[t];
      }
      return randomCode;
    }

    /// 
    /// 生成随机验证码(常用汉字)
    /// 
    /// 要产生常用汉字的个数
    /// 
    public static string CreateValidateChinese(int length)
    {
      /*gb2312中文编码表由区码和位码表示*/
      string randomCode = "";
      Random rand = new Random();
      Encoding encoding = Encoding.GetEncoding("gb2312");//从gb2312中文编码表提取汉字
      for (int i = 0; i )
      {
        int regionCode = rand.Next(16, 56); // 获取区码(常用汉字的区码范围为16-55)    
        int locationCode = rand.Next(1, (regionCode == 55 ? 90 : 95));// 获取位码(位码范围为1-94 由于55区的90,91,92,93,94为空,故将其排除)

        randomCode += encoding.GetString(new byte[] { (byte)(regionCode + 160), (byte)(locationCode + 160) });//区码位码分别加上A0H(160)的方法转换为存储码
      }
      return randomCode;
    }

    ///
    ///生成随机验证码(算术等式)
    ///
    ///验证码的长度
    ///
    public static ArithmeticEquation CreateValidateArithmetic()
    {
      Random rand = new Random();
      ArithmeticEquation ai = new ArithmeticEquation();
      ai.text = new Liststring>();
      ai.text.Add("");
      ai.text.Add("");
      ai.text.Add("");
      ai.text.Add("");

      ai.text[1] = ArithmeticSymbol[rand.Next(0, 4)];

      if (ai.text[1] == ArithmeticSymbol[0])//
      {
        int len = rand.Next(1, 3);
        if (len == 1)
        {
          ai.text[0] = rand.Next(0, 10).ToString();
        }
        else
        {
          ai.text[0] = rand.Next(1, 10).ToString();
          ai.text[0] += rand.Next(0, 10).ToString();
        }

        if (len == 1)
        {
          ai.text[2] = rand.Next(1, 10).ToString();
          ai.text[2] += rand.Next(0, 10).ToString();
        }
        else
        {
          ai.text[2] = rand.Next(0, 10).ToString();
        }
      }
      else if (ai.text[1] == ArithmeticSymbol[1])//
      {
        int len = rand.Next(1, 3);
        if (len == 1)
        {
          ai.text[0] = rand.Next(0, 10).ToString();
        }
        else
        {
          ai.text[0] = rand.Next(1, 10).ToString();
          ai.text[0] += rand.Next(0, 10).ToString();
        }

        if (len == 1)
        {
          ai.text[2] = rand.Next(0, int.Parse(ai.text[0]) + 1).ToString();
        }
        else
        {
          ai.text[2] = rand.Next(0, 10).ToString();
        }
      }
      else if (ai.text[1] == ArithmeticSymbol[2])//
      {
        ai.text[0] = rand.Next(1, 10).ToString();

        ai.text[2] = rand.Next(1, 10).ToString();
      }
      else
      {
        ai.text[2] = rand.Next(1, 10).ToString();

        ai.text[0] = (int.Parse(ai.text[2]) * rand.Next(1, 10)).ToString();

      }

      ai.value = calculate(ai.text[1], int.Parse(ai.text[0]), int.Parse(ai.text[2])).ToString();

      return ai;
    }

    ///
    ///创建验证码的图片
    ///
    ///验证码参数
    ///Gif样式
    public static Bitmap CreateValidateImage(BitmapParam bitmapParam, BitmapStyle bitmapStyle)
    {
      Bitmap result = null;
      Random random = new Random();
      string text = String.Concat(bitmapParam.textarr);
      int count = bitmapParam.textarr.Count;

      Bitmap bmp_template = new Bitmap(bitmapParam.width, bitmapParam.height);
      Graphics g_template = Graphics.FromImage(bmp_template);
      #region 绘制干扰线

      g_template.Clear(bitmapParam.backColor);
      using (Pen pen = new Pen(Color.Silver))
      {
        for (int i = 0; i 25; i++)
        {
          int x1 = random.Next(bmp_template.Width);
          int x2 = random.Next(bmp_template.Width);
          int y1 = random.Next(bmp_template.Height);
          int y2 = random.Next(bmp_template.Height);
          g_template.DrawLine(pen, x1, y1, x2, y2);
        }
      }

      #endregion

      List dotlist = new List();
      #region 计算干扰点坐标

      for (int i = 0; i 100; i++)
      {
        dotlist.Add(new Dot() { gyd = new Point(random.Next(bmp_template.Width), random.Next(bmp_template.Height)), color = Color.FromArgb(random.Next()) });
      }
      #endregion

      int defaultfontsize = 7;
      int defaultfontmargin = 3;
      #region 根据RECT计算文本字体大小

      for (int i = defaultfontsize; i 300; i++)
      {
        using (Font f = new Font(bitmapParam.fontName, i, bitmapParam.fontStyle))
        {
          if (g_template.MeasureString(text, f).Width > bitmapParam.width - defaultfontmargin * 2)
          {
            defaultfontsize = i;
            break;
          }
        }
      }
      for (int i = defaultfontsize; i > 1; i--)
      {
        using (Font f = new Font(bitmapParam.fontName, i, bitmapParam.fontStyle))
        {
          if (g_template.MeasureString(text, f).Height 2)
          {
            defaultfontsize = i;
            break;
          }
        }
      }

      #endregion
      Font text_font = new Font(bitmapParam.fontName, defaultfontsize, bitmapParam.fontStyle);
      SizeF text_size = g_template.MeasureString(text, text_font);
      LinearGradientBrush text_brush = new LinearGradientBrush(new Rectangle(0, 0, bmp_template.Width, bmp_template.Height), Color.FromArgb(193, 0, 91), Color.FromArgb(136, 193, 0), 1.2f, true);

      float x_align = ((float)bitmapParam.width - text_size.Width) / 2.0f;
      float y_align = ((float)bitmapParam.height - text_size.Height) / 2.0f;
      List bitmapframelist = new List();
      if (!bitmapStyle.IsGif || (bitmapStyle.IsGif && bitmapStyle.FrameType == GifFrameType.FullFrame))
      {
        bitmapframelist.Add(new BitmapFrame() { frame = (Bitmap)bmp_template.Clone() });
        using (Graphics graphics = Graphics.FromImage(bitmapframelist[0].frame))
        {
          graphics.DrawString(text, text_font, text_brush, x_align, y_align);
          foreach (Dot dot in dotlist)
          {
            bitmapframelist[0].frame.SetPixel(dot.gyd.X, dot.gyd.Y, dot.color);
          }
        }
        if (!bitmapStyle.IsGif)
        {
          result = bitmapframelist[0].frame;
        }
        else
        {
          GifCore gif = new GifCore(bitmapStyle.Delay, bitmapStyle.Repeat);
          foreach (Dot dot in dotlist)
          {
            bmp_template.SetPixel(dot.gyd.X, dot.gyd.Y, dot.color);
          }
          gif.AddFrame(bmp_template, delay: -1);
          gif.AddFrame((Image)bitmapframelist[0].frame, delay: -1);
          result = gif.Finish();
          foreach (BitmapFrame item in bitmapframelist)
          {
            item.frame.Dispose();
          }
        }
      }
      else
      {
        for (int i = 0; i )
        {
          bitmapframelist.Add(new BitmapFrame() { frame = (Bitmap)bmp_template.Clone() });
        }

        #region 计算每一帧文字坐标


        if (count == 1)
        {
          bitmapframelist[0].x = x_align;
          bitmapframelist[0].y = y_align;
          bitmapframelist[0].z_width = 0;
        }
        else
        {
          for (int k = 0; k )
          {
            if (k == 0)
            {
              bitmapframelist[0].x = x_align;
              bitmapframelist[0].y = y_align;
              bitmapframelist[0].z_width = g_template.MeasureString(GetScopeString(bitmapParam.textarr, 0, k + 2), text_font).Width - g_template.MeasureString(bitmapParam.textarr[k + 1], text_font).Width;
            }
            else
            {
              bitmapframelist[k].x = bitmapframelist[k - 1].x + bitmapframelist[k - 1].z_width;
              bitmapframelist[k].y = y_align;
              bitmapframelist[k].z_width = g_template.MeasureString(GetScopeString(bitmapParam.textarr, 0, k + 1), text_font).Width - g_template.MeasureString(GetScopeString(bitmapParam.textarr, 0, k), text_font).Width;
            }
          }
        }

        #endregion

        #region 绘制每一帧文字坐标

        for (int k = 0; k )
        {
          using (Graphics graphics = Graphics.FromImage(bitmapframelist[k].frame))
          {
            graphics.DrawString(bitmapParam.textarr[k], text_font, text_brush, bitmapframelist[k].x, bitmapframelist[k].y);
            foreach (Dot dot in dotlist) //画图片的前景干扰点
            {
              bitmapframelist[k].frame.SetPixel(dot.gyd.X, dot.gyd.Y, dot.color);
            }
          }
        }

        #endregion

        #region 生成Gif

        GifCore gif = new GifCore(bitmapStyle.Delay, bitmapStyle.Repeat);
        if (count == 1)
        {
          foreach (Dot dot in dotlist)
          {
            bmp_template.SetPixel(dot.gyd.X, dot.gyd.Y, dot.color);
          }
          gif.AddFrame(bmp_template, delay: -1);
        }
        foreach (BitmapFrame item in bitmapframelist)
        {
          gif.AddFrame((Image)item.frame, delay: -1);
        }
        result = gif.Finish();
        foreach (BitmapFrame item in bitmapframelist)
        {
          item.frame.Dispose();
        }

        #endregion
      }

      text_font.Dispose();
      text_brush.Dispose();
      g_template.Dispose();
      bmp_template.Dispose();
      return result;

    }

    //
    ///
    ///创建验证码的图片
    ///
    ///验证码参数
    ///Gif样式
    public static byte[] CreateValidateImageBytes(BitmapParam bitmapParam, BitmapStyle bitmapStyle)
    {
      using (MemoryStream ms = new MemoryStream())
      {
        Bitmap image = CreateValidateImage(bitmapParam, bitmapStyle);
        image.Save(ms, bitmapStyle.IsGif ? ImageFormat.Gif : ImageFormat.Jpeg);
        if (image != null)
          image.Dispose();
        return ms.ToArray();
      }
    }

    //
    ///
    ///创建验证码的图片
    ///
    ///验证码参数
    ///Gif样式
    public static string CreateValidateImageBase64String(BitmapParam bitmapParam, BitmapStyle bitmapStyle)
    {
      return String.Format(imageStr, bitmapStyle.IsGif ? "gif" : "jpeg") + Convert.ToBase64String(CreateValidateImageBytes(bitmapParam, bitmapStyle));
    }

    /// 
    /// 拆分文本
    /// 
    /// 
    /// 
    public static Liststring> StringToArray(string str)
    {
      str = str.Trim();
      Liststring> result = new Liststring>();
      for (int i = 0; i )
      {
        result.Add(str.Substring(i, 1));
      }
      return result;
    }

    /// 
    ///获取数组指定范围字符 
    /// 
    /// 
    /// 
    /// 
    /// 
    public static string GetScopeString(Liststring> arr, int start, int len)
    {
      string result = String.Empty;
      for (int i = 0; i )
      {
        result += arr[start + i];
      }
      return result;
    }

    /// 
    /// 计算
    /// 
    /// 
    /// 
    /// 
    /// 
    private static int calculate(string type, int value1, int value2)
    {
      if (type == ArithmeticSymbol[0])//
      {
        return value1 + value2;
      }
      else if (type == ArithmeticSymbol[1])//
      {
        return value1 - value2;
      }
      else if (type == ArithmeticSymbol[2])//
      {
        return value1 * value2;
      }
      else
      {
        return value1 / value2;
      }
    }

    /// 
    /// 干扰点
    /// 
    private class Dot
    {
      public Point gyd;
      public Color color;
    }

    /// 
    /// 图片帧
    /// 
    private class BitmapFrame
    {
      /// 
      ////// 
      public Bitmap frame;
      public float x;
      public float y;
      public float z_width;
    }

    /// 
    /// 验证码(算术等式)
    /// 
    public class ArithmeticEquation
    {
      /// 
      /// 算术等式
      /// 
      public Liststring> text;
      /// 
      /// 算术值
      /// 
      public string value;
    }

    /// 
    /// Gif帧类型
    /// 
    public enum GifFrameType
    {
      /// 
      /// 每个字符一帧
      /// 
      EveryFrame,
      /// 
      /// 全部字符一帧
      /// 
      FullFrame
    }

    /// 
    /// 验证码参数
    /// 
    public class BitmapParam
    {
      public BitmapParam()
      {

      }

      public BitmapParam(Liststring> _textarr, int _width, int _height, Color _backColor, string _fontName, Color _fontBrushStrat, Color _fontBrushEnd, FontStyle _fontStyle)
      {
        this.textarr = _textarr;
        this.width = _width;
        this.height = _height;
        this.backColor = _backColor;
        this.fontName = _fontName;
        this.fontBrushStrat = _fontBrushStrat;
        this.fontBrushEnd = _fontBrushEnd;
        this.fontStyle = _fontStyle;
      }

      /// 
      /// 验证码字符集
      /// 
      public Liststring> textarr;
      /// 
      /// 验证码图片宽
      /// 
      public int width = 120;
      /// 
      /// 验证码图片高
      /// 
      public int height = 50;

      /// 
      /// 图片背景颜色
      /// 
      public Color backColor = Color.White;

      /// 
      /// 字体
      /// 
      public string fontName = "微软雅黑";

      /// 
      /// 字体画笔颜色开始
      /// 
      public Color fontBrushStrat = Color.FromArgb(193, 0, 91);

      /// 
      /// 字体画笔颜色结束
      /// 
      public Color fontBrushEnd = Color.FromArgb(136, 193, 0);

      /// 
      /// 验证码字体样式
      /// 
      public FontStyle fontStyle = FontStyle.Bold | FontStyle.Italic;
    }

    /// 
    /// Gif样式
    /// 
    public class BitmapStyle
    {
      public BitmapStyle()
      {

      }

      public BitmapStyle(bool _IsGif, GifFrameType _FrameType, int _Delay, int _Repeat)
      {
        this.IsGif = _IsGif;
        this.FrameType = _FrameType;
        this.Delay = _Delay;
        this.Repeat = _Repeat;
      }

      /// 
      /// 是否生成Gif动画
      /// 
      public bool IsGif = false;
      /// 
      ///  Gif帧类型
      /// 
      public GifFrameType FrameType = GifFrameType.EveryFrame;
      /// 
      /// 帧之间的延迟(毫秒)
      /// 
      public int Delay = 500;
      /// 
      /// GIF重复计数(0表示永久)
      /// 
      public int Repeat = 0;
    }
  }

 

 

 源码下载地址:c# 随机验证码

C#生成随机验证码

标签:encoding   foreach   ref   ppa   return   字体   als   inf   die   

原文地址:https://www.cnblogs.com/tlmbem/p/10800943.html


评论


亲,登录后才可以留言!