完全copy的网上的代码,总不显示图片,也不报错,感觉没有调用checkcode.ashx。
将随机数用常量测试,也一样
将图片用现成的图片测试,也一样
路径都调整在根目录下了调用页login.aspx中
<img src="checkcode.ashx" alt="nothing" width="100px" height="100px" />处理页中
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string code = GenerateCheckCode();
        context.Session["code"] = code;
        this.CreateCheckCodeImage(code, context);
    }
    private string GenerateCheckCode()
    {
        int number;
        char code;
        string checkCode = String.Empty;        System.Random random = new Random();        for (int i = 0; i < 4; i++)
        {
            number = random.Next();            if (number % 2 == 0)
                code = (char)('0' + (char)(number % 10));
            else
                code = (char)('A' + (char)(number % 26));            checkCode += code.ToString();
        }
        return checkCode;
    }    private void CreateCheckCodeImage(string checkCode, HttpContext context)
    {
        if (checkCode == null || checkCode.Trim() == String.Empty)
            return;        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
        Graphics g = Graphics.FromImage(image);        try
        {
            //生成随机生成器
            Random random = new Random();            //清空图片背景色
            g.Clear(Color.White);            //画图片的背景噪音线
            for (int i = 0; i < 5; 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("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            g.DrawString(checkCode, font, brush, 2, 2);            //画图片的前景噪音点
            for (int i = 0; i < 100; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }            //画图片的边框线
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            context.Response.ClearContent();
            context.Response.ContentType = "image/Gif";
            context.Response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    } 

解决方案 »

  1.   

    会不会问题出在login.aspx上,或者要设置web.config用asp:image控件调用,也没有显示
      

  2.   

    你直接访问这个ashx 是什么效果?图片是X吗?
      

  3.   

    把 try finally 注释掉看有无异常。
      

  4.   

    图片是X,还有文字nothing
      

  5.   

    把 try finally 注释掉看有无异常。
    没变化,不起作用
      

  6.   

    用一个HyperLink 直接访问ashx,闪退一下
      

  7.   

    你的代码是可以显示出验证码的,请检查你访问的url(包括aspx)是不是https的,如果是,请改成context.Response.ContentType = "image/jpeg";我记得在https下context.Response.ContentType = "image/Gif"是有问题的,显示不出图片(具体是https下不行还是页面和ashx一个在http下,一个在https下不行,记不清楚了,有空你自已试一下。  )
      

  8.   

    要么是你犯了低级错误<img src="checkcode.ashx" alt="nothing" width="100px" height="100px" />src地址你写错了。你用浏览器直接访问checkcode.ashx,如果可以看到图片,那就是没问题。
      

  9.   

    还没登录验证通过,用浏览器直接访问checkcode.ashx出错,又返回该页。将checkcode.ashx文件用ie 打开时,报如下无法显示 XML 页。 
    无法查看使用 XSL 样式表的 XML 输入。请更正错误然后单击 刷新 按钮,或稍后重试。 
    --------------------------------------------------------------------------------名称以无效字符开头。处理资源 'file:///D:/ptb00/ptb00/checkcode.ashx' 时出错。第 1 行,位置: 2 <%@ WebHandler Language="C#" Class="checkcode" %>
    -^
     
      

  10.   

    我4了一下,先登录进去,再访问checkcode.ashx,,的确可以看到图片
      

  11.   

    没叫你用浏览器直接访问硬盘上的文件,,你用http://127.0.0.1/checkcode.ashx这样的url去访问checkcode.ashx,晕。 
      

  12.   

    没试过这种写法,正常img的src应该是图片的路径,要跳转的话加click事件,然后在click事件中访问checkcode.ashx