我做了个生成验证码的页面code.aspx(从网上抄的)后台cs代码如下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;public partial class checkCode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateCheckCodeImage(GenerateCheckCode());//调用下面两个方法;
    }    /// <summary>
    /// 生成验证码的随机数
    /// </summary>
    /// <returns>返回五位随机数</returns>
    private string GenerateCheckCode()
    {
        int number;
        char code;
        string checkCode = String.Empty;        Random random = new Random();        for (int i = 0; i < 5; i++)//可以任意设定生成验证码的位数
        {
            number = random.Next();            if (number % 2 == 0)
                code = (char)('0' + (char)(number % 10));
            else
                code = (char)('A' + (char)(number % 26));            checkCode += code.ToString();
        }        Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));//写入COOKIS
        Session["CheckCode"] = checkCode;  //写入Session,可以任意选一下
        return checkCode;
    }
    /// <summary>
    /// 生成验证码图片
    /// </summary>
    /// <param name="checkCode"></param>
    private void CreateCheckCodeImage(string checkCode)
    {
        if (checkCode == null || checkCode.Trim() == String.Empty)
            return;        Bitmap image = new 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 < 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("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            LinearGradientBrush brush = new 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);            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        }
        finally
        {//释放对象资源
            g.Dispose();
            image.Dispose();
        }
    }}
前台没有写任何东西。
我在另一页面login.aspx引用这个code.aspx页面<asp:Image runat=server ID="img1" ImageUrl="~/checkcode.aspx" />但在login.aspx页面上显示的却是一个红X,不能显示验证码,刚开始我以为是没有验证就不能执行代码,把web.config的安全验证取消,但还是一样不能显示,请各位大虾帮找找看是什么原因,谢谢!!

解决方案 »

  1.   

    可以从51ASPX下载一个源码看看,有一个论坛也用这个验证码,原理一样的,看看吧
      

  2.   


    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Drawing;public partial class CheckCodeImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {  
         
                Response.Expires = 0;
                Response.CacheControl = "no-cache";            // Session["VNum"] = GenerateCheckCode();
                this.CreateCheckCodeImage(GenerateCheckCode());
            }
        }
        private string GenerateCheckCode()
        {
            int number;
            char code;
            string checkCode = String.Empty;        System.Random random = new Random();        for (int i = 0; i < 5; i++)
            {
                number = random.Next(0, 99);            if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));            checkCode += code.ToString();
            }        Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));        return checkCode;
        }    private void CreateCheckCodeImage(string checkCode)
        {
            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 < 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("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);
                Response.ClearContent();
                Response.ContentType = "image/Gif";
                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
    }
    <asp:Image runat=server ID="img1" ImageUrl="~/checkcode.aspx" / > 
    路径自己要设置好!  应该不成问题 !!!
      

  3.   

    详细看了下! protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {  
         
                Response.Expires = 0;
                Response.CacheControl = "no-cache";上面的这2句你少了 !            // Session["VNum"] = GenerateCheckCode();
                this.CreateCheckCodeImage(GenerateCheckCode());
            }
        }
      

  4.   

    ImageUrl="~/checkcode.aspx" 路径不对 而且都不需要(浪费服务器资源)使用服务器图片控件
     ImageUrl= checkcode.aspx 
    ok!!!!!!!!!!!!!!
      

  5.   

    原因找到了,是namespace的问题,这里没有加到命名空间,所以运行时找不到。还有public partial class CheckCodeImage : System.Web.UI.Page,class后面的名称应和页面名称一致。谢谢大家!!
      

  6.   

    点点兄弟,不好意思,你第一贴的代码太长了,结贴时看不到给分的输入框,所以没有给你分,你到这里来接分吧http://topic.csdn.net/u/20071022/11/b36f5f43-9b8d-4f68-81c1-48352bde796b.html