C#生成验证码 ------生成方法一 pageload(object sender, eventarges e) { string chkCode = string.Empty; //颜色列表,用于验证码、噪线、噪点 Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue }; //字体列表,用于验证码 string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" }; //验证码的字符集,去掉了一些容易混淆的字符 char[] character = { ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘P‘, ‘R‘, ‘S‘, ‘T‘, ‘W‘, ‘X‘, ‘Y‘ }; Random rnd = new Random(); //生成验证码字符串 for (int i = 0; i < 4; i++) { chkCode += character[rnd.Next(character.Length)]; } Bitmap bmp = new Bitmap(100, 40); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); //画噪线 for (int i = 0; i < 10; i++) { int x1 = rnd.Next(100); int y1 = rnd.Next(40); int x2 = rnd.Next(100); int y2 = rnd.Next(40); Color clr = color[rnd.Next(color.Length)]; g.DrawLine(new Pen(clr), x1, y1, x2, y2); } //画验证码字符串 for (int i = 0; i < chkCode.Length; i++) { string fnt = font[rnd.Next(font.Length)]; Font ft = new Font(fnt, 18); Color clr = color[rnd.Next(color.Length)]; g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8); } //画噪点 for (int i = 0; i < 100; i++) { int x = rnd.Next(bmp.Width); int y = rnd.Next(bmp.Height); Color clr = color[rnd.Next(color.Length)]; bmp.SetPixel(x, y, clr); } //清除该页输出缓存,设置该页无缓存 Response.Buffer = true; Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0); Response.Expires = 0; Response.CacheControl = "no-cache"; Response.AppendHeader("Pragma", "No-Cache"); //将验证码图片写入内存流,并将其以 "image/Png" 格式输出 MemoryStream ms = new MemoryStream(); try { bmp.Save(ms, ImageFormat.Png); Response.ClearContent(); Response.ContentType = "image/Png"; //Response.BinaryWrite(ms.ToArray()); } finally { //显式释放资源 bmp.Dispose(); g.Dispose(); } } ------生成方法二 <%@ WebHandler Language="C#" class="yzCodeHandler" %> using System; using System.Web; using System.Drawing; public class yzCodeHandler : IHttpHandler , System.Web.SessionState.IRequiresSessionState { public string charSet = "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"; public void ProcessRequest (HttpContext context) { string validateCode = CreateRandomCode(4); context.Session["ValidateCode"] = validateCode; CreateImage(validateCode, context); } public bool IsReusable { get { return false; } } /// <summary> /// 生成验证码 /// <param name="n">位数</param> /// <returns>验证码字符串</returns> private string CreateRandomCode(int n) { string[] CharArray = charSet.Split(‘,‘); string randomCode = ""; int temp = -1; Random rand = new Random(); for (int i = 0; i < n; i++) { if (temp != -1) { rand = new Random(i * temp * ((int)DateTime.Now.Ticks)); } int t = rand.Next(CharArray.Length - 1); if (temp == t) { return CreateRandomCode(n); } temp = t; randomCode += CharArray[t]; } return randomCode; } private void CreateImage(string checkCode, HttpContext context) { int iwidth = (int)(checkCode.Length * 13); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23); Graphics g = Graphics.FromImage(image); Font f = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Bold)); // 前景色 Brush b = new System.Drawing.SolidBrush(Color.Black); // 背景色 g.Clear(Color.White); // 填充文字 g.DrawString(checkCode, f, b, 0, 1); // 随机线条 Pen linePen = new Pen(Color.Gray, 0); Random rand = new Random(); for (int i = 0; i < 5; i++) { int x1 = rand.Next(image.Width); int y1 = rand.Next(image.Height); int x2 = rand.Next(image.Width); int y2 = rand.Next(image.Height); g.DrawLine(linePen, x1, y1, x2, y2); } // 随机点 for (int i = 0; i < 30; i++) { int x = rand.Next(image.Width); int y = rand.Next(image.Height); image.SetPixel(x, y, Color.Gray); } // 边框 g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1); // 输出图片 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); context.Response.ClearContent(); context.Response.ContentType = "image/Jpeg"; context.Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); } } ---------------- 验证方法 if (txtValidateCode.Text.ToUpper().Trim() == Session["ValidateCode"].ToString().ToUpper().Trim()) { //true } else { //false } 分类: C#
时间: 2024-11-01 11:15:24