求大神给个简单的验证码代码,4个字符的,最好显示的大小不一,谢谢了!!!
1 string chkCode = string.Empty; 2 3 //颜色列表,用于验证码、噪线、噪点 4 5 Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Brown, Color.Brown, Color.DarkBlue }; 6 7 //字体列表,用于验证码 8 9 string[] font = { "Gungsuh" }; 10 11 //验证码的字符集,去掉了一些容易混淆的字符 12 13 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(); 14 15 //生成验证码字符串 16 17 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); 18 19 //画验证码字符串 20 21 for (int i = 0; i < chkCode.Length; i++) { string fnt = font[rnd.Next(font.Length)]; Font ft = new Font(fnt, 22); Color clr = color[rnd.Next(color.Length)]; g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8); } 22 23 //清除该页输出缓存,设置该页无缓存 24 25 Response.Buffer = true; Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0); Response.Expires = 0; Response.CacheControl = "no-cache"; Response.AppendHeader("Pragma", "No-Cache"); 26 27 //将验证码图片写入内存流,并将其以 "image/Png" 格式输出 28 29 MemoryStream ms = new MemoryStream(); 30 31 try { bmp.Save(ms, ImageFormat.Png); 32 33 Session["CheckCode"] = chkCode; Response.ClearContent(); 34 35 Response.ContentType = "image/Png"; Response.BinaryWrite(ms.ToArray()); } finally { 36 37 //显式释放资源 38 39 bmp.Dispose(); 40 41 g.Dispose(); }
出来混,自己也要动动脑子,大小不一样,你自己可以再改,
可以结贴了,不谢
验证码一般不是大小问题,而是加一些横线而已。