1.建立ValidateCode.aspx页面cs代码
None.gifpublic class ValidateCode : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif    简单验证码的产生_ide_04{
InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        简单验证码的产生_ide_04{
InBlock.gif            //如果要在页面a.aspx生成验证码,则在该页面添加一个图片控件,假设命名为:Image1,然后在page_Load事件中写如下代码:
InBlock.gif            //ImageButton1.ImageUrl = "ValidateCode.aspx";
InBlock.gif            //这样就可以生成验证码了,ValidateCode.aspx页面可以随便放在哪里,不过要注意Image1.src 要写对,同级可以直接写ValidateCode.aspx,上一级写../ValidateCode.aspx,很方便吧。
InBlock.gif
InBlock.gif            if(!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            简单验证码的产生_ide_04{
InBlock.gif                //RndNum是一个自定义函数
InBlock.gif                //这里的数字4代表显示的是4位的验证字符串!
InBlock.gif                //string VNum=RndNum(4); 
InBlock.gif                string VNum=GenerateRandom(4);
InBlock.gif                Session["VNum"] = VNum;
InBlock.gif                Validate_Code(VNum);
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        Web Form Designer generated codeWeb Form Designer generated code#region Web Form Designer generated code
InBlock.gif        override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        简单验证码的产生_ide_04{
InBlock.gif            //
InBlock.gif            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            //
InBlock.gif            InitializeComponent();
InBlock.gif            base.OnInit(e);
ExpandedSubBlockEnd.gif        }
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**//**//// <summary>
InBlock.gif        /// Required method for Designer support - do not modify
InBlock.gif        /// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        简单验证码的产生_ide_04{    
InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
InBlock.gif        private void Validate_Code(string VNum) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        简单验证码的产生_ide_04{
InBlock.gif            int Gheight=(int)(VNum.Length * 11.5);
InBlock.gif            //gheight为图片宽度,根据字符长度自动更改图片宽度
InBlock.gif            System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight,20);
InBlock.gif            Graphics g = Graphics.FromImage(Img);
InBlock.gif            g.DrawString(VNum,new System.Drawing.Font("Arial",10),new System.Drawing.SolidBrush(Color.Red),3,3); 
InBlock.gif            //在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y) 
InBlock.gif            System.IO.MemoryStream ms=new System.IO.MemoryStream();
InBlock.gif            Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png); 
InBlock.gif            Response.ClearContent(); //需要输出图象信息 要修改HTTP头 
InBlock.gif            Response.ContentType="image/Png";
InBlock.gif            Response.BinaryWrite(ms.ToArray());
InBlock.gif            g.Dispose();
InBlock.gif            Img.Dispose(); 
InBlock.gif            Response.End();
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        private static char[] constant=
ExpandedSubBlockStart.gifContractedSubBlock.gif        简单验证码的产生_ide_04{
InBlock.gif            '0','1','2','3','4','5','6','7','8','9',
InBlock.gif            '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',
InBlock.gif            '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'
ExpandedSubBlockEnd.gif        };
InBlock.gif        public static string GenerateRandom(int Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif        简单验证码的产生_ide_04{   
InBlock.gif            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
InBlock.gif            Random rd= new Random();
InBlock.gif            for(int i=0;i<Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            简单验证码的产生_ide_04{
InBlock.gif                newRandom.Append(constant[rd.Next(62)]);
ExpandedSubBlockEnd.gif            }
InBlock.gif            return newRandom.ToString();
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedBlockEnd.gif    }

2.建立演示页面Login.aspx,html代码
None.gif<HTML>
None.gif    <HEAD>
None.gif        <title>Login</title>
None.gif        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
None.gif        <meta name="CODE_LANGUAGE" Content="C#">
None.gif        <meta name="vs_defaultClientScript" content="JavaScript">
None.gif        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
None.gif    </HEAD>
None.gif    <body>
None.gif        <form id="Form1" method="post" runat="server">
None.gif            <table align="center" cellSpacing="0" cellPadding="0" width="100%" border="0" height="100%">
None.gif                <colgroup>
None.gif                    <col width="30%">
None.gif                    </col>
None.gif                    <col width="40%">
None.gif                    </col>
None.gif                    <col width="30%">
None.gif                    </col>
None.gif                </colgroup>
None.gif                <tr>
None.gif                    <td></td>
None.gif                    <td valign="middle">
None.gif                        <TABLE id="Table1" align="center" cellSpacing="0" cellPadding="0" width="100%" border="0">
None.gif                            <colgroup>
None.gif                                <col width="20%">
None.gif                                </col>
None.gif                                <col width="35%">
None.gif                                </col>
None.gif                                <col width="25%">
None.gif                                </col>
None.gif                                <col width="20%">
None.gif                                </col>
None.gif                            </colgroup>
None.gif                            <TR>
None.gif                                <TD align="right">LoginName</TD>
None.gif                                <TD>
None.gif                                    <asp:TextBox id="txtLoginName" runat="server" Width="100%"></asp:TextBox></TD>
None.gif                                <td></td>
None.gif                                <td></td>
None.gif                            </TR>
None.gif                            <TR>
None.gif                                <TD align="right">Password</TD>
None.gif                                <TD>
None.gif                                    <asp:TextBox id="txtPassword" runat="server" Width="100%"></asp:TextBox></TD>
None.gif                                <td></td>
None.gif                                <td></td>
None.gif                            </TR>
None.gif                            <TR>
None.gif                                <TD align="right">ValidateCode</TD>
None.gif                                <TD>
None.gif                                    <asp:TextBox id="txtValidateCode" runat="server" Width="100%"></asp:TextBox></TD>
None.gif                                <td><IMG alt="" src="Pages/Wonderful/Form/ValidateCode.aspx"></td>
None.gif                                <td></td>
None.gif                            </TR>
None.gif                            <TR>
None.gif                                <TD></TD>
None.gif                                <TD>
None.gif                                    <asp:Button id="Login" runat="server" Text="Login"></asp:Button>
None.gif                                    <asp:Button id="Reset" runat="server" Text="Reset"></asp:Button></TD>
None.gif                                <td></td>
None.gif                                <td></td>
None.gif                            </TR>
None.gif                        </TABLE>
None.gif                    </td>
None.gif                    <td></td>
None.gif                </tr>
None.gif            </table>
None.gif        </form>
None.gif    </body>
None.gif</HTML>

3.Login页面的cs代码
None.gifpublic class Login : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif简单验证码的产生_ide_04{
InBlock.gif    protected System.Web.UI.WebControls.TextBox txtLoginName;
InBlock.gif    protected System.Web.UI.WebControls.TextBox txtPassword;
InBlock.gif    protected System.Web.UI.WebControls.TextBox txtValidateCode;
InBlock.gif    protected System.Web.UI.WebControls.Button Login;
InBlock.gif    protected System.Web.UI.WebControls.Button Reset;
InBlock.gif    protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gifprivate void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        简单验证码的产生_ide_04{
InBlock.gif            if(Session["VNum"]!=null && Session["VNum"].ToString()==txtValidateCode.Text)
ExpandedSubBlockStart.gifContractedSubBlock.gif            简单验证码的产生_ide_04{
InBlock.gif                //验证码正确
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        Web Form Designer generated codeWeb Form Designer generated code#region Web Form Designer generated code
InBlock.gif        override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        简单验证码的产生_ide_04{
InBlock.gif            //
InBlock.gif            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            //
InBlock.gif            InitializeComponent();
InBlock.gif            base.OnInit(e);
ExpandedSubBlockEnd.gif        }
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//**//**//// <summary>
InBlock.gif        /// Required method for Designer support - do not modify
InBlock.gif        /// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        简单验证码的产生_ide_04{    
InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ExpandedBlockEnd.gif}