int intLength = 5;
    string strIdentify = "Identify";
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/gif";
        Bitmap b = new Bitmap(200, 60);
        Graphics g = Graphics.FromImage(b);
        g.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 200, 60);
        Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
        Random r = new Random();        string strLetters = "abcedfhijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < intLength; i++)
        {
            s.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
            g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(Color.Blue), i * 38, r.Next(0, 15));        }        Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
        for (int i = 0; i < 10; i++)
        {
            g.DrawLine(pen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
        }        b.Save(context.Response.OutputStream, ImageFormat.Gif);        context.Session[strIdentify] = s.ToString();
        context.Response.End();
    }这段代码是放在一般处理程序.ashx里面的。
是用来产生一段验证码的。
可是 context.Session[strIdentify] = s.ToString();
上面这段代码会报错。
“未将对象引用设置到对象的实例。”

解决方案 »

  1.   

    呵呵。我是照着一人的博客写的。
    链接地址:
    http://blog.csdn.net/ChengKing/archive/2007/01/05/1475140.aspx
      

  2.   

    我也是网上抄的..
    我现在是这样的..可以运行成功<%@ WebHandler Language="C#" Class="Handler" %>
    using System;
    using System.Web;
    using System.Web.SessionState;
    using System.Text;
    using System.Drawing;
    using System.Drawing.Imaging;public class Handler : IHttpHandler, IRequiresSessionState 
    {    public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/gif";
            //建立Bitmap对象,绘图
            Bitmap basemap = new Bitmap(50, 20);
            Graphics graph = Graphics.FromImage(basemap);
            graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 50, 20);
            Font font = new Font(FontFamily.GenericSerif, 16, FontStyle.Bold, GraphicsUnit.Pixel);
            Random r = new Random();
            string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
            string letter;
            StringBuilder s = new StringBuilder();        //添加随机的五个字母
            for (int x = 0; x < 4; x++)
            {
                letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
                s.Append(letter);
                graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 12, r.Next(0, 5));
            }        //混淆背景
            Pen linePen = new Pen(new SolidBrush(Color.Black),1);
            for (int x = 0; x < 2; x++)
                graph.DrawLine(linePen, new Point(r.Next(0, 49), r.Next(0, 19)), new Point(r.Next(0, 49), r.Next(0, 19)));        //将图片保存到输出流中
            basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
            context.Session["str_yan"] = s.ToString();   //如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片
            context.Response.End();
        }
     
        public bool IsReusable 
        {
            get 
            {
                return false;
            }
        }}
      

  3.   

    噢,原来还需要继承irequiressessionstate.
    不过我把这句话给注释了是可以用的。
    只不过就不能验证输得对不对了。。
      

  4.   

    因为有些大小写及数字..生成时经常看不清楚所以验证码只 ABCDEFGHIJKLMNPQRSTUVWXYZ
    这些