汉字验证码

闲来无事,就写了一个汉字验证码,不常见吧,废话少说直接上代码:

View Code
  1  protected void Page_Load(object sender, EventArgs e)
  2     {
  3         int length = 4;
  4         string strkey = getString(length);
  5         byte[] data = CheckCodeService(length, strkey);
  6         Response.OutputStream.Write(data, 0, data.Length);
  7     }
  8     //产生的汉字验证码
  9     private object[] CreateString(int strLength) { 
 10       //定义一个数组存储汉字编码的组成元素
 11         string[] str = new string[16] {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f" };
 12         Random ran = new Random();
 13         object[] bytes = new object[strLength];
 14         for (int i = 0; i < strLength; i++) { 
 15          //获取区位码第一位
 16             int ran1 = ran.Next(11, 14);
 17             string str1 = str[ran1].Trim();
 18             //获取第二位并防止重复
 19             ran = new Random(ran1*unchecked((int)DateTime.Now.Ticks)+i);
 20             int ran2;
 21             if (ran1 == 13)
 22             {
 23                 ran2 = ran.Next(0, 7);
 24             }
 25             else
 26             {
 27                 ran2 = ran.Next(0, 16);
 28             }
 29             string str2 = str[ran2].Trim();
 30             //获取第三位区位码
 31             ran = new Random(ran2*unchecked((int)DateTime.Now.Ticks)+i);
 32             int ran3 = ran.Next(10,16);
 33             string str3 = str[ran3].Trim();
 34            //获取第四位区位码
 35             ran = new Random(ran3 * unchecked((int)DateTime.Now.Ticks)+i);
 36             int ran4;
 37             if (ran3 == 10)
 38             {
 39                 ran4 = ran.Next(1, 16);
 40             }
 41             else if (ran3 == 15)
 42             {
 43                 ran4 = ran.Next(0, 15);
 44             }
 45             else
 46 
 47             {
 48                 ran4 = ran.Next(0,16);
 49             }
 50             string str4 = str[ran4].Trim();
 51             //定义字节变量存储产生的随机汉字区位码
 52             byte byte1 = Convert.ToByte(str1+str2,16);
 53             byte byte2 = Convert.ToByte(str3+str4,16);
 54             byte[] stradd = new byte[] {byte1,byte2};
 55             //将产生的汉字放入数组
 56             bytes.SetValue(stradd, i);
 57 
 58 
 59         }
 60         return bytes;
 61     
 62     
 63     }
 64 
 65     //获取验证码
 66     private string getString(int length) {
 67         Encoding gb = Encoding.GetEncoding("gb2312");
 68         object[] bytes = CreateString(length);
 69         //根据汉字字节解码出中文汉字
 70         string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0],typeof(byte[])));
 71         string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
 72         string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
 73         string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
 74         string str = str1 + str2 + str3 + str4;
 75         Response.Cookies.Add(new HttpCookie("CheckCode",str));
 76         return str;
 77     }
 78 
 79     //生成图片验证码
 80     protected byte[] CheckCodeService(int nLen, string strKey)
 81     {
 82         int nBmpWidth = 13 * nLen + 50;
 83         int nBmpHeight = 25;
 84         System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(nBmpWidth, nBmpHeight);
 85         //生成随机背景颜色
 86         int nRed;
 87         int nGreen;
 88         int nBlue;
 89         //生成三原色
 90         System.Random rd = new Random((int)System.DateTime.Now.Ticks);
 91         nRed = rd.Next(255) % 128 + 128;
 92         nGreen = rd.Next(255) % 128 + 128;
 93         nBlue = rd.Next(255) % 128 + 128;
 94         //填充背景
 95         System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
 96         graph.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.AliceBlue), 0, 0, nBmpWidth, nBmpHeight);
 97         //绘制干扰线,采用比背景深一些的颜色
 98         int nLines = 3;
 99         System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(nRed - 17, nGreen - 17, nBlue - 17), 2);
100         for (int a = 0; a < nLines; a++)
101         {
102             int x1 = rd.Next(nBmpWidth);
103             int x2 = rd.Next(nBmpHeight);
104             int y1 = rd.Next(nBmpWidth);
105             int y2 = rd.Next(nBmpHeight);
106             graph.DrawLine(pen, x1, y1, x2, y2);
107 
108         }
109         //画图片的前景噪音点
110         for (int i = 0; i < 100; i++)
111         {
112             int x = rd.Next(bmp.Width);
113             int y = rd.Next(bmp.Height);
114             bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(rd.Next()));
115 
116         }
117         //确定字体
118         System.Drawing.Font font = new System.Drawing.Font("Courier New", 14 + rd.Next() % 4, System.Drawing.FontStyle.Bold);
119         System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true);
120         graph.DrawString(strKey, font, brush, 2, 2);
121         //输出字节流
122         System.IO.MemoryStream stream = new System.IO.MemoryStream();
123         bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
124         bmp.Dispose();
125         graph.Dispose();
126         byte[] byteReturn = stream.ToArray();
127         stream.Close();
128         return byteReturn;
129     }
原文地址:https://www.cnblogs.com/ypyhy/p/2881625.html