//===============a.ashx代码如下:========================
<%@ WebHandler Language="C#" Class="ValidateImageHandler" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Data;
/// <summary>
/// ValidateImageHandler 生成网站验证码功能
/// </summary>
public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
{
    int intLength = 5;               //长度
    string strIdentify = "Identify"; //随机字串存储键值,以便存储到Session中
public ValidateImageHandler()
{
}    /// <summary>
    ///  生成验证图片核心代码
    /// </summary>
    /// <param name="hc"></param>
    public void ProcessRequest(HttpContext hc)
    {
        //设置输出流图片格式
        hc.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, 42, FontStyle.Bold, GraphicsUnit.Pixel);
        Random r = new Random();        //合法随机显示字符列表
        string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        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(hc.Response.OutputStream, ImageFormat.Gif);
        hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用户输入是否一致
//Response.Write(hc.Session[strIdentify]);
//Response.End();
        hc.Response.End();
    }
    
    /// <summary>
    /// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
    /// </summary>
    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
}
前台显示的页面b.aspx如下:
<img width="100px" height="25px" src="ValidateImageHandler.ashx"/>我怎么才能使用hc.Session[strIdentify] = s.ToString();中的数值?

解决方案 »

  1.   

    上面的不要看了,直接看这里,怎么知道这里的验证码是哪个字符串(也就是s.ToString()怎么使用的问题,我想写入Session["a"]=s.ToString(),可这样不能写)?<%@ Page Language="C#"  ResponseEncoding="gb2312"%>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.SessionState" %>
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.Drawing.Imaging" %>
    <%@ Import Namespace="System.Data" %>
    <script runat="server">
    public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
    {
        int intLength = 5;               //长度
        string strIdentify = "Identify"; //随机字串存储键值,以便存储到Session中
        public void ProcessRequest(HttpContext hc)
        {
            //设置输出流图片格式
            hc.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, 42, FontStyle.Bold, GraphicsUnit.Pixel);
            Random r = new Random();
            //合法随机显示字符列表
            string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            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(hc.Response.OutputStream, ImageFormat.Gif);
            hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用户输入是否一致
    hc.Response.End();
        }
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
    </script>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>验证码</title>
    </head>
    <body>
       <img width="100px" height="25px" src="ValidateImageHandler.ashx"/></body>
    </html>
      

  2.   

    我用
    <%Response.Write("验证码="+Session["Identify"]);%>写出的是上一次的验证码
      

  3.   

    你们直接复制下面的这段看看,我输出的是上次显示的验证码(也就是第一次不记录session)<%@ Page Language="C#"  ResponseEncoding="gb2312"%>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.SessionState" %>
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.Drawing.Imaging" %>
    <%@ Import Namespace="System.Data" %>
    <script runat="server">
    public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
    {
        int intLength = 5;             
        public void ProcessRequest(HttpContext hc)
        {
            //设置输出流图片格式
            hc.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, 42, FontStyle.Bold, GraphicsUnit.Pixel);
            Random r = new Random();
            //合法随机显示字符列表
            string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            StringBuilder s = new StringBuilder();   
    hc.Session["Identify"] = s.ToString(); //先保存在Session中,验证与用户输入是否一致
        
            //将随机生成的字符串绘制到图片上
            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(hc.Response.OutputStream, ImageFormat.Gif);
            
        }
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
    </script>
    <%Response.Write("验证码="+Session["Identify"]);%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>验证码</title>
    </head>
    <body>
    <form name="form1" method="post" action="1.aspx">
       <img width="100px" height="25px" src="ValidateImageHandler.ashx"/>
       <input type="text" name="textfield">
       <input type="hidden" name="a" value="<%Response.Write(Session["Identify"]);%>">
       <input type="submit" name="Submit" value="提交">
    </form> 
    </body>
    </html>