标题:新手asp编程的基本法则 [新手必看] 2009-6-17 更新
只看楼主
luoye820116
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-10-10
得分:0 
public class ValidCode : IHttpHandler
{
    //添加生成验证码的随机函数
    private string GetValidCode(int num)
    {

        string strRandomCode = "ABCD1EF2GH3IJ4KL5MN6P7QR8ST9UVWXYZ";                                //定义要随机抽取的字符串
        char[] chastr = strRandomCode.ToCharArray();                                                //将定义的字符串转成字符数组
        StringBuilder sbValidCode = new StringBuilder();                                            //定义StringBuilder对象用于存放验证码
        Random rd = new Random();                                                                   //随机函数,随机抽取字符
        for (int i = 0; i < num; i++)
        {
            //以strRandomCode的长度产生随机位置并截取该位置的字符添加到StringBuilder对象中
            sbValidCode.Append(strRandomCode.Substring(rd.Next(0, strRandomCode.Length), 1));
        }
        return sbValidCode.ToString();

    }
   
    //生成验证码
   
    public void ProcessRequest (HttpContext context)
    {
        string strValidCode = GetValidCode(5);                                                  // 产生5位随机字符
        context.Session["ValidCode"] = strValidCode;                                            //将字符串保存到Session中,以便需要时进行验证
        Bitmap image = new Bitmap(120, 30);                                                     //定义宽120像素,高30像素的数据定义的图像对象
        Graphics g = Graphics.FromImage(image);                                                 //绘制图片
        try
        {

            Random random = new Random();                                                       //生成随机对象
            g.Clear(Color.White);                                                               //清除图片背景色
            for (int i = 0; i < 25; i++)                                                        // 随机产生图片的背景噪线
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);
                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

            Font font = new System.Drawing.Font("新宋体", 20, (System.Drawing.FontStyle.Bold));  //设置图片字体风格
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 3, true);//设置画笔类型
            g.DrawString(strValidCode, font, brush, 5, 2);                                      //绘制随机字符


            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);    //绘制图片的前景噪点
             ms = new ();                           //建立存储区为内存的流
            image.Save(ms, ImageFormat.Gif);                                                    //将图像对象储存为内存流
            context.Response.ClearContent();                                                    //清除当前缓冲区流中的所有内容
            context.Response.ContentType = "image/Gif";                                         //设置输出流的MIME类型
            context.Response.BinaryWrite(ms.ToArray());                                         //将内存流写入到输出流
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
    }

    public bool IsReusable
    {
        get {
            return false;
        }
    }

}
我写的这段代码运行时 context.Session["ValidCode"] = strValidCode;位置提示System.NullReferenceException错误,怎么改
2010-10-10 15:45
wxb0326
Rank: 1
等 级:新手上路
帖 子:5
专家分:5
注 册:2009-6-8
得分:0 
赶紧出下一期啊
2010-10-14 16:17
insilout
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-8-27
得分:0 
呵呵,不错哦
2010-10-15 21:12
叮当1212
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-10-17
得分:0 
很好很强大。。。
2010-10-17 16:59
moonjoy
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-11-2
得分:0 
谢谢,我学习了!
2010-11-02 15:19
zoujh0101
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2010-11-5
得分:0 
hehe
很不错啊,收藏了,希望版主继续啊。。。
2010-11-05 15:29
zhanxinlin
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:96
专家分:156
注 册:2010-5-19
得分:0 
一直在观注,却久未见更新了
2010-11-11 01:33
oscar2003
Rank: 1
等 级:等待验证会员
帖 子:2
专家分:0
注 册:2008-9-23
得分:0 
收下好好学习,谢谢版主
2010-11-23 09:08
byxiaofan
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-11-26
得分:0 
新来看了学习下
2010-11-26 16:41
sjzfls
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-5-11
得分:0 
这样的好文章我们必须支持啊 !
2010-11-29 09:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-217539-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053186 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved