如题,讨论一下,有过这方面经验的老大有空出来指点一下。

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;
    using System.IO;
    using System.Web.UI;namespace NewWeb
    {
    /// <summary>
    /// ValidateCode 的摘要说明。
    ///校验码类:生成5位数字校验码图片。
    /// </summary>
    public class ValidateCode
    {
    private Bitmap ValidateImage;
    private Graphics  g;
    private int AreaWidth;
    private int AreaHeight;
    private int AreaFontSize;
    private string AreaFamilyName; public ValidateCode()
    {
    init();
    ValidateImage=new Bitmap(AreaWidth,AreaHeight,PixelFormat.Format24bppRgb);
    g=Graphics.FromImage(ValidateImage);
    } private void init()
    {
    this.AreaWidth=56;
    this.AreaHeight=18;
    this.AreaFontSize=11;
    this.AreaFamilyName="Verdana";
    } public void SetValidateStyle(int width,int height,string familyName,int fontSize)
    {
    this.AreaWidth=width;
    this.AreaHeight=height;
    this.AreaFamilyName=familyName;
    this.AreaFontSize=fontSize;
    } public void DrawValidateCode(Page e,string i)
    {
    g.FillRectangle(new LinearGradientBrush(new Point(0,0),new Point(AreaWidth,AreaHeight),Color.FromArgb(255,255,255,255),Color.FromArgb(255,255,255,255)),0,0,120,30);


    //添加干扰色
    Random r=new Random();
    for(int j=0;j<80;j++)
    {
    Pen p=new Pen(Color.FromArgb(r.Next(0,255),r.Next(0,255),r.Next(0,255)));
    int PointW=r.Next(0,AreaWidth);
    int PointH=r.Next(0,AreaHeight);
    g.DrawLine(p,PointW,PointH,PointW+1,PointH); }
    g.DrawString(i,new Font(AreaFamilyName,AreaFontSize,FontStyle.Bold),new SolidBrush(Color.Black),new PointF(0,0));
    g.Save();
    MemoryStream ms=new MemoryStream();
    ValidateImage.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
       
    e.Response.ClearContent();
    e.Response.ContentType="image/gif";
    e.Response.BinaryWrite(ms.ToArray());
    e.Response.End();
    } public string  RandomBigString()
    {
    char[] s = new char[]{'0','1', '2','3','4','5','6','7','8','9','a'
     ,'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q'
     ,'r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G'
     ,'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W'
     ,'X','Y','Z'};
    string num = "";
    Random r = new Random();
    for(int i = 0; i < 5; i++)
    {
    num += s[r.Next(0, s.Length)].ToString(); 
    }
    return num;
    } public string RandownSmaillString()
    {
    char[] s = new char[]{'1', '2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'};
    string num = "";
    Random r = new Random();
    for(int i = 0; i < 5; i++)
    {
    num += s[r.Next(0, s.Length)].ToString(); 
    }
    return num; } }
    }
      

  2.   

    http://lijunming.cnblogs.com/articles/355052.html
    http://lijunming.cnblogs.com/articles/358409.html转别人的~~看看有没有用
      

  3.   

    验证码?
    http://www.mikecat.net/blogview.asp?logID=1259&cateID=1
    NickLee.Web.UI.Capach...什么控件可以
      

  4.   

    http://blog.csdn.net/qqwwee_com/archive/2006/02/09/595607.aspx
      

  5.   

    http://blog.csdn.net/Qqwwee_Com/archive/2005/08/14/454335.aspx
      

  6.   

    webwait(webwei)的那段代码有用吗? 我试着好像不行呢。谁可以告诉我。 给分
      

  7.   

    看看这个,生成验证码的,我一直用的,还有问题在找我
     Bitmap newBitmap = new Bitmap(86, 26, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(newBitmap);
            Random r = new Random();
            g.Clear(Color.White);
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, 86, 26), Color.Red, Color.Blue, 1.2f, true);
            
            for (int i = 0; i < 25; i++)
            {
                int x1 = r.Next(newBitmap.Width);
                int x2 = r.Next(newBitmap.Width);
                int y1 = r.Next(newBitmap.Height);
                int y2 = r.Next(newBitmap.Height);
                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }
            for (int i = 0; i < 100; i++)
            {
                int x = r.Next(newBitmap.Width);
                int y = r.Next(newBitmap.Height);
                newBitmap.SetPixel(x, y, Color.FromArgb(r.Next()));
            }
            string value = GenerateRandom(4);
            Font font = new Font("黑体", 14, System.Drawing.FontStyle.Bold);
            g.DrawString(value, font, brush,2, 2);
            g.DrawRectangle(new Pen(Color.Silver),0,0,85,25);
            //g.FillRectangle(new SolidBrush(Color.Green), 0, 12, 80, 1);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            newBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/gif";
            Response.BinaryWrite(ms.ToArray());
        }
        private static char[] constant =
            {
                '0','1','2','3','4','5','6','7','8','9',
                'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','
            };
        public static string GenerateRandom(int Length)
        {
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(47);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(47)]);
            }
            return newRandom.ToString();
        }
      

  8.   

    上面的页面是validatecode.aspx
    在你需要用的页面,比如登录,添加一个Image控件,它的ImageUrl='validatecode.aspx',就可以了。
    上面漏了一句
     string value = GenerateRandom(4);
    这里添加一句 Session["code"] = value;
    然后你需要验证的页面,比如Login.aspx需要输入验证码的textbox
    if(TextBox1.Text==Session["code"].ToString())
    {
      //成功.....
    }
      

  9.   

    webwait(webwei)的那段代码有用吗? 我试着好像不行呢。谁可以告诉我。 给分
    ====
    1.显示验证码的页面:
    ....
    if(!Page.IsPostBack)
    {
    this.TextCode.Text="";
    ValidateCode myCode=new ValidateCode();
    Session["Vcode"]=myCode.RandomBigString();//或者 RandownSmaillString();
    this.Image1.ImageUrl="Vcode.aspx";
    }
    ....2.输出图像的页面(Vcode.aspx)的Page_Load里:
    ValidateCode myCode=new ValidateCode();
    myCode.DrawValidateCode(this,Session["Vcode"].ToString());
      

  10.   

    甜葡萄:你好 http://community.csdn.net//Expert/TopicView2.asp?id=4734851&datebasetype=now你提供的这个验证码我 测试了 可以成功 但是 有时会提示索引越界 
    另外 我 在页面添加textbox 也看不到 为何?请教如何解决??谢谢 打扰了
      

  11.   

    TO:hooyke(红旗下的蛋) 
      我知道那个越界的问题 越界是因为你的constant 字符数组个数不匹配   “甜葡萄”的字符数组只给出了36个 所以你改为
      public static string GenerateRandom(int Length)
        {
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(36);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(36)]);
            }
            return newRandom.ToString();
        }
    就OK了使用IMAGE控件:“上面的页面是validatecode.aspx
    在你需要用的页面,比如登录,添加一个Image控件,它的ImageUrl='validatecode.aspx',就可以了。”