zoukankan      html  css  js  c++  java
  • 验证码类

      1 /// <summary>
      2     /// 验证码类
      3     /// </summary>
      4     public class Rand
      5     {
      6         #region 生成随机数字
      7         /// <summary>
      8         /// 生成随机数字
      9         /// </summary>
     10         /// <param name="length">生成长度</param>
     11         public static string Number(int Length)
     12         {
     13             return Number(Length, false);
     14         }
     15 
     16         /// <summary>
     17         /// 生成随机数字
     18         /// </summary>
     19         /// <param name="Length">生成长度</param>
     20         /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
     21         public static string Number(int Length, bool Sleep)
     22         {
     23             if (Sleep) System.Threading.Thread.Sleep(3);
     24             string result = "";
     25             System.Random random = new Random();
     26             for (int i = 0; i < Length; i++)
     27             {
     28                 result += random.Next(10).ToString();
     29             }
     30             return result;
     31         }
     32         #endregion
     33 
     34         #region 生成随机字母与数字
     35         /// <summary>
     36         /// 生成随机字母与数字
     37         /// </summary>
     38         /// <param name="IntStr">生成长度</param>
     39         public static string Str(int Length)
     40         {
     41             return Str(Length, false);
     42         }
     43 
     44         /// <summary>
     45         /// 生成随机字母与数字
     46         /// </summary>
     47         /// <param name="Length">生成长度</param>
     48         /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
     49         public static string Str(int Length, bool Sleep)
     50         {
     51             if (Sleep) System.Threading.Thread.Sleep(3);
     52             char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
     53             string result = "";
     54             int n = Pattern.Length;
     55             System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
     56             for (int i = 0; i < Length; i++)
     57             {
     58                 int rnd = random.Next(0, n);
     59                 result += Pattern[rnd];
     60             }
     61             return result;
     62         }
     63         #endregion
     64 
     65         #region 生成随机纯字母随机数
     66         /// <summary>
     67         /// 生成随机纯字母随机数
     68         /// </summary>
     69         /// <param name="IntStr">生成长度</param>
     70         public static string Str_char(int Length)
     71         {
     72             return Str_char(Length, false);
     73         }
     74 
     75         /// <summary>
     76         /// 生成随机纯字母随机数
     77         /// </summary>
     78         /// <param name="Length">生成长度</param>
     79         /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
     80         public static string Str_char(int Length, bool Sleep)
     81         {
     82             if (Sleep) System.Threading.Thread.Sleep(3);
     83             char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
     84             string result = "";
     85             int n = Pattern.Length;
     86             System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
     87             for (int i = 0; i < Length; i++)
     88             {
     89                 int rnd = random.Next(0, n);
     90                 result += Pattern[rnd];
     91             }
     92             return result;
     93         }
     94         #endregion
     95     }
     96 
     97     /// <summary>
     98     /// 验证图片类
     99     /// </summary>
    100     public class YZMHelper
    101     {
    102         #region 私有字段
    103         private string text;
    104         private Bitmap image;
    105         private int letterWidth = 16;  //单个字体的宽度范围
    106         private int letterHeight = 20; //单个字体的高度范围
    107         private static byte[] randb = new byte[4];
    108         private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
    109         private Font[] fonts = 
    110     {
    111        new Font(new FontFamily("Times New Roman"),10 +Next(1),System.Drawing.FontStyle.Regular),
    112        new Font(new FontFamily("Georgia"), 10 + Next(1),System.Drawing.FontStyle.Regular),
    113        new Font(new FontFamily("Arial"), 10 + Next(1),System.Drawing.FontStyle.Regular),
    114        new Font(new FontFamily("Comic Sans MS"), 10 + Next(1),System.Drawing.FontStyle.Regular)
    115     };
    116         #endregion
    117 
    118         #region 公有属性
    119         /// <summary>
    120         /// 验证码
    121         /// </summary>
    122         public string Text
    123         {
    124             get { return this.text; }
    125         }
    126 
    127         /// <summary>
    128         /// 验证码图片
    129         /// </summary>
    130         public Bitmap Image
    131         {
    132             get { return this.image; }
    133         }
    134         #endregion
    135 
    136         #region 构造函数
    137         public YZMHelper()
    138         {
    139             //HttpContext.Current.Response.Expires = 0;
    140             //HttpContext.Current.Response.Buffer = true;
    141             //HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
    142             //HttpContext.Current.Response.AddHeader("pragma", "no-cache");
    143             //HttpContext.Current.Response.CacheControl = "no-cache";
    144             this.text = Rand.Number(4);
    145             CreateImage();
    146         }
    147         #endregion
    148 
    149         #region 私有方法
    150         /// <summary>
    151         /// 获得下一个随机数
    152         /// </summary>
    153         /// <param name="max">最大值</param>
    154         private static int Next(int max)
    155         {
    156             rand.GetBytes(randb);
    157             int value = BitConverter.ToInt32(randb, 0);
    158             value = value % (max + 1);
    159             if (value < 0) value = -value;
    160             return value;
    161         }
    162 
    163         /// <summary>
    164         /// 获得下一个随机数
    165         /// </summary>
    166         /// <param name="min">最小值</param>
    167         /// <param name="max">最大值</param>
    168         private static int Next(int min, int max)
    169         {
    170             int value = Next(max - min) + min;
    171             return value;
    172         }
    173         #endregion
    174 
    175         #region 公共方法
    176         /// <summary>
    177         /// 绘制验证码
    178         /// </summary>
    179         public void CreateImage()
    180         {
    181             int int_ImageWidth = this.text.Length * letterWidth;
    182             Bitmap image = new Bitmap(int_ImageWidth, letterHeight);
    183             Graphics g = Graphics.FromImage(image);
    184             g.Clear(Color.White);
    185             for (int i = 0; i < 2; i++)
    186             {
    187                 int x1 = Next(image.Width - 1);
    188                 int x2 = Next(image.Width - 1);
    189                 int y1 = Next(image.Height - 1);
    190                 int y2 = Next(image.Height - 1);
    191                 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
    192             }
    193             int _x = -12, _y = 0;
    194             for (int int_index = 0; int_index < this.text.Length; int_index++)
    195             {
    196                 _x += Next(12, 16);
    197                 _y = Next(-2, 2);
    198                 string str_char = this.text.Substring(int_index, 1);
    199                 str_char = Next(1) == 1 ? str_char.ToLower() : str_char.ToUpper();
    200                 Brush newBrush = new SolidBrush(GetRandomColor());
    201                 Point thePos = new Point(_x, _y);
    202                 g.DrawString(str_char, fonts[Next(fonts.Length - 1)], newBrush, thePos);
    203             }
    204             for (int i = 0; i < 10; i++)
    205             {
    206                 int x = Next(image.Width - 1);
    207                 int y = Next(image.Height - 1);
    208                 image.SetPixel(x, y, Color.FromArgb(Next(0, 255), Next(0, 255), Next(0, 255)));
    209             }
    210             image = TwistImage(image, true, Next(1, 3), Next(4, 6));
    211             g.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, (letterHeight - 1));
    212             this.image = image;
    213         }
    214 
    215         /// <summary>
    216         /// 字体随机颜色
    217         /// </summary>
    218         public Color GetRandomColor()
    219         {
    220             Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
    221             System.Threading.Thread.Sleep(RandomNum_First.Next(50));
    222             Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
    223             int int_Red = RandomNum_First.Next(180);
    224             int int_Green = RandomNum_Sencond.Next(180);
    225             int int_Blue = (int_Red + int_Green > 300) ? 0 : 400 - int_Red - int_Green;
    226             int_Blue = (int_Blue > 255) ? 255 : int_Blue;
    227             return Color.FromArgb(int_Red, int_Green, int_Blue);
    228         }
    229 
    230         /// <summary>
    231         /// 正弦曲线Wave扭曲图片
    232         /// </summary>
    233         /// <param name="srcBmp">图片路径</param>
    234         /// <param name="bXDir">如果扭曲则选择为True</param>
    235         /// <param name="nMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
    236         /// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
    237         public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
    238         {
    239             double PI = 6.283185307179586476925286766559;
    240             Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
    241             Graphics graph = Graphics.FromImage(destBmp);
    242             graph.FillRectangle(new SolidBrush(Color.White), 0, 0, destBmp.Width, destBmp.Height);
    243             graph.Dispose();
    244             double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
    245             for (int i = 0; i < destBmp.Width; i++)
    246             {
    247                 for (int j = 0; j < destBmp.Height; j++)
    248                 {
    249                     double dx = 0;
    250                     dx = bXDir ? (PI * (double)j) / dBaseAxisLen : (PI * (double)i) / dBaseAxisLen;
    251                     dx += dPhase;
    252                     double dy = Math.Sin(dx);
    253                     int nOldX = 0, nOldY = 0;
    254                     nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
    255                     nOldY = bXDir ? j : j + (int)(dy * dMultValue);
    256 
    257                     Color color = srcBmp.GetPixel(i, j);
    258                     if (nOldX >= 0 && nOldX < destBmp.Width
    259                      && nOldY >= 0 && nOldY < destBmp.Height)
    260                     {
    261                         destBmp.SetPixel(nOldX, nOldY, color);
    262                     }
    263                 }
    264             }
    265             srcBmp.Dispose();
    266             return destBmp;
    267         }
    268         #endregion
  • 相关阅读:
    Android 重写系统Crash处理类,保存Crash信息到SD卡 和 完美退出程序的方法
    LeetCode第二十四题-交换链表中节点值
    LeetCode第二十三题-合并n个有序链表
    LeetCode第二十二题-创建n对括号
    LeetCode第二十一题-对两个有序链表排序
    LeetCode第二十题-有效的括号
    LeetCode第十九题-链表节点的删除
    LeetCode第十八题-四数之和
    LeetCode第十七题-电话号码的字母组合
    LeetCode第十六题-找出数组中三数之和最接近目标值的答案
  • 原文地址:https://www.cnblogs.com/xzitluyang/p/5897243.html
Copyright © 2011-2022 走看看