| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 571 人关注过本帖
标题:求这个程序出现错误怎么改
取消只看楼主 加入收藏
luoye820116
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-10-10
结帖率:40%
收藏
已结贴  问题点数:20 回复次数:2 
求这个程序出现错误怎么改
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错误,怎么改
搜索更多相关主题的帖子: class 函数 验证码 private public 
2010-10-10 15:53
luoye820116
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-10-10
收藏
得分:0 
那个对象没有实例化?
2010-10-12 21:05
luoye820116
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-10-10
收藏
得分:0 
我在CSS文件里定义了一个框架格式
*FLASH*/
#flash{
    height: 126px;
    width: 986px;
    margin-bottom: 4px;
}
/*FLASH 结束*/
但是在.aspx文件中引用调用时,flash文件运行不起来什么原因?后台.aspx代码如下
<div id="flash">
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="top.swf" width="986" height="126">
        <param name="movie" value="01/images/top.swf" />
        <param name="quality" value="high" />
        <embed src="01/images/top.swf" quality="high" pluginspage="http://www. type="application/x-shockwave-flash" width="986" height="126"></embed>
        </object>
</div>
2010-10-12 21:14
快速回复:求这个程序出现错误怎么改
数据加载中...
 
   



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

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