Asp.net无刷新中文验证码调试成功,特分享给大家


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

核心源码

 1 None.gif      public  partial  class  Image : System.Web.UI.Page
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif    protected void Page_Load(object sender, EventArgs e)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 5InBlock.gif        CreateCheckCodeImage(GenCode(4));
 6ExpandedSubBlockEnd.gif    }

 7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
 8InBlock.gif    /// '产生随机字符串
 9InBlock.gif    /// </summary>
10InBlock.gif    /// <param name="num">随机出几个字符</param>
11ExpandedSubBlockEnd.gif    /// <returns>随机出的字符串</returns>

12InBlock.gif    private string GenCode(int num)
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14InBlock.gif        string str = "的一是在不了有和人这中大为上个国我以要他时来用们...";
15InBlock.gif        char[] chastr = str.ToCharArray();
16InBlock.gif       
17InBlock.gif        string code = "";
18InBlock.gif        Random rd = new Random();
19InBlock.gif        int i;
20InBlock.gif        for (i = 0; i < num; i++)
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            //code += source[rd.Next(0, source.Length)];
23InBlock.gif            code += str.Substring(rd.Next(0, str.Length), 1);
24ExpandedSubBlockEnd.gif        }

25InBlock.gif        return code;
26InBlock.gif
27ExpandedSubBlockEnd.gif    }

28InBlock.gif
29ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//**//**//// <summary>
30InBlock.gif    /// 生成图片(增加背景噪音线、前景噪音点)
31InBlock.gif    /// </summary>
32ExpandedSubBlockEnd.gif    /// <param name="checkCode">随机出字符串</param>

33InBlock.gif    private void CreateCheckCodeImage(string checkCode)
34ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
35InBlock.gif        if (checkCode.Trim() == "" || checkCode == null)
36InBlock.gif            return;
37InBlock.gif        Session["Code"= checkCode; //将字符串保存到Session中,以便需要时进行验证
38InBlock.gif        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)(checkCode.Length * 21.5), 22);
39InBlock.gif        Graphics g  = Graphics.FromImage(image);
40InBlock.gif        try
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            //生成随机生成器
43InBlock.gif            Random random = new Random(); 
44InBlock.gif
45InBlock.gif            //清空图片背景色
46InBlock.gif            g.Clear(Color.White);  
47InBlock.gif
48InBlock.gif            // 画图片的背景噪音线
49InBlock.gif            int i;
50InBlock.gif            for (i = 0; i < 25; i++)
51ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
52InBlock.gif                int x1 = random.Next(image.Width);
53InBlock.gif                int x2 = random.Next(image.Width);
54InBlock.gif                int y1 = random.Next(image.Height);
55InBlock.gif                int y2 = random.Next(image.Height);
56InBlock.gif                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
57ExpandedSubBlockEnd.gif            }

58InBlock.gif
59InBlock.gif            Font font = new System.Drawing.Font("Arial"12, (System.Drawing.FontStyle.Bold));
60InBlock.gif            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2Ftrue);
61InBlock.gif            g.DrawString(checkCode, font, brush, 22);
62InBlock.gif
63InBlock.gif            //画图片的前景噪音点
64InBlock.gif            g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);
65InBlock.gif            System.IO.MemoryStream ms = new System.IO.MemoryStream();
66InBlock.gif            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
67InBlock.gif            Response.ClearContent();
68InBlock.gif            Response.ContentType = "image/Gif";
69InBlock.gif            Response.BinaryWrite(ms.ToArray());
70InBlock.gif
71ExpandedSubBlockEnd.gif        }

72InBlock.gif        catch
73ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
74InBlock.gif            g.Dispose();
75InBlock.gif            image.Dispose();
76ExpandedSubBlockEnd.gif        }

77InBlock.gif
78ExpandedSubBlockEnd.gif    }

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值