Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)delv
published in(发表于) 2014/1/23 3:12:16
Asp.net无刷新中文验证码调试成功,特分享给大家_[Asp.Net教程]

Asp.net无刷新中文验证码调试成功,特分享给大家_[Asp.Net教程]

以前在网上找到了很多关于中文验证码的文章(园子里也有,大家自己去找吧),但是都没有调测成功,总出现
The target '__Page' for the callback could not be found or did not implement ICallbackEventHandler不能ICallbackEventHandler回掉的错误,我进行了一下修正并整理,现在可以实现了中文验证码无刷新的操作,现特把全部源码分享给大家

核心源码


1 public partial class Image : System.Web.UI.Page
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 CreateCheckCodeImage(GenCode(4));
6 }
7 /**//**//**////


8 /// '产生随机字符串
9 ///

10 /// 随机出几个字符
11 /// 随机出的字符串
12 private string GenCode(int num)
13 {
14 string str = "的一是在不了有和人这中大为上个国我以要他时来用们...";
15 char[] chastr = str.ToCharArray();
16
17 string code = "";
18 Random rd = new Random();
19 int i;
20 for (i = 0; i < num; i++)
21 {
22 //code += source[rd.Next(0, source.Length)];
23 code += str.Substring(rd.Next(0, str.Length), 1);
24 }
25 return code;
26
27 }
28
29 /**//**//**////
30 /// 生成图片(增加背景噪音线、前景噪音点)
31 ///

32 /// 随机出字符串
33 private void CreateCheckCodeImage(string checkCode)
34 {
35 if (checkCode.Trim() == "" || checkCode == null)
36 return;
37 Session["Code"] = checkCode; //将字符串保存到Session中,以便需要时进行验证
38 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)(checkCode.Length * 21.5), 22);
39 Graphics g = Graphics.FromImage(image);
40 try
41 {
42 //生成随机生成器
43 Random random = new Random();
44
45 //清空图片背景色
46 g.Clear(Color.White);
47
48 // 画图片的背景噪音线
49 int i;
50 for (i = 0; i < 25; i++)
51 {
52 int x1 = random.Next(image.Width);
53 int x2 = random.Next(image.Width);
54 int y1 = random.Next(image.Height);
55 int y2 = random.Next(image.Height);
56 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
57 }
58
59 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
60 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);
61 g.DrawString(checkCode, font, brush, 2, 2);
62
63 //画图片的前景噪音点
64 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
65 System.IO.MemoryStream ms = new System.IO.MemoryStream();
66 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
67 Response.ClearContent();
68 Response.ContentType = "image/Gif";
69 Response.BinaryWrite(ms.ToArray());
70
71 }
72 catch
73 {
74 g.Dispose();
75 image.Dispose();
76 }
77
78 }


希望该源码只是一个抛砖引玉的作用,你可以进行修改,比如说改中文字库,字体背景噪音等等
默认帐号密码均为51aspx,这里用户登录只是一个验证的例子,没有其他功能


作者51aspx
完整源码下载地址http://www.51aspx.com/CV/ZhongWenYanZhengMa







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759