C#生成中文汉字验证码(源码)

时间:2022-08-31 12:36:56
1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Collections;
  5 using System.Web;
  6 using System.Web.Security;
  7 using System.Web.UI;
  8 using System.Web.UI.WebControls;
  9 using System.Web.UI.WebControls.WebParts;
 10 using System.Web.UI.HtmlControls;
 11 using System.Drawing;
 12 using System.Text;
 13 public partial class Admin_ValidCode : System.Web.UI.Page
 14 {
 15 protected void Page_Load(object sender, EventArgs e)
 16 {
 17 this.CreateCheckCodeImage(GenerateCheckCode());
 18 }
 19 private string GenerateCheckCode()
 20 {
 21 int number;
 22 //char code;
 23 string checkCode = String.Empty;
 24 System.Random random = new Random();
 25 Encoding gb = Encoding.GetEncoding("gb2312");
 26 //调用函数产生4个随机中文汉字编码
 27 object[] bytes = CreateRegionCode(6);
 28 //根据汉字编码的字节数组解码出中文汉字
 29 string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
 30 string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
 31 string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
 32 string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
 33 //输出的控制台
 34 checkCode = str1 + str2 + str3 + str4;
 35 //for (int i = 0; i < 5; i++)
 36 //{
 37 // number = random.Next();
 38 // if (number % 2 == 0)
 39 // code = (char)('0' + (char)(number % 10));
 40 // else
 41 // code = (char)('A' + (char)(number % 26));
 42 // checkCode += code.ToString();
 43 //}
 44 Response.Cookies.Add(new HttpCookie("ValidCode",LjfLib.Encrypt.AES.EncryptAes(checkCode)));
 45 //Session["ValidCode"] = checkCode;
 46 
 47 return checkCode;
 48 }
 49 #region 产生波形滤镜效果
 50 private static object[] CreateRegionCode(int strlength)
 51 {
 52 //定义一个字符串数组储存汉字编码的组成元素
 53 string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
 54 Random rnd = new Random();
 55 //定义一个object数组用来
 56 object[] bytes = new object[strlength];
 57 
 58 for (int i = 0; i < strlength; i++)
 59 {
 60 //区位码第1位
 61 int r1 = rnd.Next(11, 14);
 62 string str_r1 = rBase[r1].Trim();
 63 //区位码第2位
 64 rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的
 65 //种子避免产生重复值
 66 int r2;
 67 if (r1 == 13)
 68 {
 69 r2 = rnd.Next(0, 7);
 70 }
 71 else
 72 {
 73 r2 = rnd.Next(0, 16);
 74 }
 75 string str_r2 = rBase[r2].Trim();
 76 //区位码第3位
 77 rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
 78 int r3 = rnd.Next(10, 16);
 79 string str_r3 = rBase[r3].Trim();
 80 //区位码第4位
 81 rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
 82 int r4;
 83 if (r3 == 10)
 84 {
 85 r4 = rnd.Next(1, 16);
 86 }
 87 else if (r3 == 15)
 88 {
 89 r4 = rnd.Next(0, 15);
 90 }
 91 else
 92 {
 93 r4 = rnd.Next(0, 16);
 94 }
 95 string str_r4 = rBase[r4].Trim();
 96 //定义两个字节变量存储产生的随机汉字区位码
 97 byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
 98 byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
 99 //将两个字节变量存储在字节数组中
100 byte[] str_r = new byte[] { byte1, byte2 };
101 //将产生的一个汉字的字节数组放入object数组中
102 bytes.SetValue(str_r, i);
103 }
104 return bytes;
105 }
106 private const double PI = 3.1415926535897932384626433832795;
107 private const double PI2 = 6.283185307179586476925286766559;
108 private System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
109 {
110 System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
111 // 将位图背景填充为白色
112 System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
113 graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
114 graph.Dispose();
115 double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
116 for (int i = 0; i < destBmp.Width; i++)
117 {
118 for (int j = 0; j < destBmp.Height; j++)
119 {
120 double dx = 0;
121 dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
122 dx += dPhase;
123 double dy = Math.Sin(dx);
124 // 取得当前点的颜色
125 int nOldX = 0, nOldY = 0;
126 nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
127 nOldY = bXDir ? j : j + (int)(dy * dMultValue);
128 System.Drawing.Color color = srcBmp.GetPixel(i, j);
129 if (nOldX >= 0 && nOldX < destBmp.Width
130 && nOldY >= 0 && nOldY < destBmp.Height)
131 {
132 destBmp.SetPixel(nOldX, nOldY, color);
133 }
134 }
135 }
136 return destBmp;
137 }
138 #endregion
139 private void CreateCheckCodeImage(string checkCode)
140 {
141 if (checkCode == null    checkCode.Trim() == String.Empty)
142 return;
143 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 27.0)), 25);
144 Graphics g = Graphics.FromImage(image);
145 try
146 {
147 //生成随机生成器
148 Random random = new Random();
149 //清空图片背景色
150 g.Clear(Color.White);
151 //画图片的背景噪音线
152 for (int i = 0; i < 12; i++)
153 {
154 int x1 = random.Next(image.Width);
155 int x2 = random.Next(image.Width);
156 int y1 = random.Next(image.Height);
157 int y2 = random.Next(image.Height);
158 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
159 }
160 //Font font = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Bold   System.Drawing.FontStyle.Italic));
161 Font font = new System.Drawing.Font("楷体_GB2312", 16, (System.Drawing.FontStyle.Bold));
162 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
163 g.DrawString(checkCode, font, brush, 2, 2);
164 //画图片的前景噪音点
165 for (int i = 0; i < 100; i++)
166 {
167 int x = random.Next(image.Width);
168 int y = random.Next(image.Height);
169 image.SetPixel(x, y, Color.FromArgb(random.Next()));
170 }
171 //image = TwistImage(image, true, 3, 1);
172 //画图片的波形滤镜效果
173 //画图片的边框线
174 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
175 System.IO.MemoryStream ms = new System.IO.MemoryStream();
176 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
177 Response.ClearContent();
178 Response.ContentType = "image/Gif";
179 Response.BinaryWrite(ms.ToArray());
180 }
181 finally
182 {
183 g.Dispose();
184 image.Dispose();
185 }
186 }
187 }