先照着网上找的代码做了一个Validcode.aspx.cs页,然后在另一个页面上拉了一个TEXTBOX控件,在后面加了一个 <iframe src="Validcode.aspx" height="25" width="100" scrolling="no" frameborder="0"  marginwidth="0" marginheight="0" style="width: 60px; height: 22px"></iframe>
浏览时就可以显示出验证码来了
然后我在后台加代码
 if (this.TextBox1.Text == Session["ValidCode"].ToString()) { Response.Write("验证成功!"); }
   if (this.TextBox1.Text == Session["ValidCode"].ToString()) { Response.Write("验证成功!"); } else   {    Response.Write("验证失败!");   }
        else   {    Response.Write("验证失败!");   }出现错误:未将对象引用设置到对象的实例。
然后把代码去掉,又在页面上加了<IMG src="Validcode.aspx"/>
奇怪的事发生了,两张图片竟然显示不一样的验证码!!!到底要添哪一个???
我的前台页面
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="yzm.aspx.cs" Inherits="yzm" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="yzm.aspx.cs" Inherits="yzm" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!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">
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
<head runat="server"> <title>无标题页</title>
    <title>无标题页</title> </head>
</head> <body>
<body> <form id="form1" runat="server">
    <form id="form1" runat="server"> <div>
        <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <iframe src="Validcode.aspx" height="25" width="100" scrolling="no" frameborder="0"
            <iframe src="Validcode.aspx" height="25" width="100" scrolling="no" frameborder="0" marginwidth="0" marginheight="0" style="width: 60px; height: 22px"></iframe>
                marginwidth="0" marginheight="0" style="width: 60px; height: 22px"></iframe> <IMG src="Validcode.aspx"/>
                <IMG src="Validcode.aspx"/>
 </div>
        </div> </form>
    </form> </body>
</body> </html>
</html>
Validcode.aspx.cs页
 using System;
using System; using System.Data;
using System.Data; using System.Configuration;
using System.Configuration; using System.Collections;
using System.Collections; using System.Web;
using System.Web; using System.Web.Security;
using System.Web.Security; using System.Web.UI;
using System.Web.UI; using System.Web.UI.WebControls;
using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls; using System.Drawing;
using System.Drawing; using System.Text;
using System.Text;
 public partial class validcode : System.Web.UI.Page
public partial class validcode : System.Web.UI.Page {
{ private void Page_Load(object sender, System.EventArgs e)
    private void Page_Load(object sender, System.EventArgs e) {
    { this.CreateCheckCodeImage(GenerateCheckCode());
        this.CreateCheckCodeImage(GenerateCheckCode()); }
    }
 Web 窗体设计器生成的代码
    Web 窗体设计器生成的代码
 private string GenerateCheckCode()
    private string GenerateCheckCode() {
    { int number;
        int number; char code;
        char code; string checkCode = String.Empty;
        string checkCode = String.Empty;
 System.Random random = new Random();
        System.Random random = new Random();
 for (int i = 0; i < 5; i++)
        for (int i = 0; i < 5; i++) {
        { number = random.Next();
            number = random.Next();
 if (number % 2 == 0)
            if (number % 2 == 0) code = (char)('0' + (char)(number % 10));
                code = (char)('0' + (char)(number % 10)); else
            else code = (char)('A' + (char)(number % 26));
                code = (char)('A' + (char)(number % 26));
 checkCode += code.ToString();
            checkCode += code.ToString(); }
        }
 //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
        //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); Session["ValidCode"] = checkCode;
        Session["ValidCode"] = checkCode; return checkCode;
        return checkCode; }
    }
 private void CreateCheckCodeImage(string checkCode)
    private void CreateCheckCodeImage(string checkCode) {
    { if (checkCode == null || checkCode.Trim() == String.Empty)
        if (checkCode == null || checkCode.Trim() == String.Empty) return;
            return;
 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); Graphics g = Graphics.FromImage(image);
        Graphics g = Graphics.FromImage(image);
 try
        try {
        { //生成随机生成器
            //生成随机生成器 Random random = new Random();
            Random random = new Random();
 //清空图片背景色
            //清空图片背景色 g.Clear(Color.White);
            g.Clear(Color.White);
 //画图片的背景噪音线
            //画图片的背景噪音线 for (int i = 0; i < 25; i++)
            for (int i = 0; i < 25; i++) {
            { int x1 = random.Next(image.Width);
                int x1 = random.Next(image.Width); int x2 = random.Next(image.Width);
                int x2 = random.Next(image.Width); int y1 = random.Next(image.Height);
                int y1 = random.Next(image.Height); int y2 = random.Next(image.Height);
                int y2 = random.Next(image.Height);
 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                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));
            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);
            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);
            g.DrawString(checkCode, font, brush, 2, 2);
 //画图片的前景噪音点
            //画图片的前景噪音点 for (int i = 0; i < 100; i++)
            for (int i = 0; i < 100; i++) {
            { int x = random.Next(image.Width);
                int x = random.Next(image.Width); int y = random.Next(image.Height);
                int y = random.Next(image.Height);
 image.SetPixel(x, y, Color.FromArgb(random.Next()));
                image.SetPixel(x, y, Color.FromArgb(random.Next())); }
            }
 //画图片的边框线
            //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
 System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent();
            Response.ClearContent(); Response.ContentType = "image/Gif";
            Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray());
            Response.BinaryWrite(ms.ToArray()); }
        } finally
        finally {
        { g.Dispose();
            g.Dispose(); image.Dispose();
            image.Dispose(); }
        } }
    } }
}

 
 
                    
                     
                    
                 
                    
                 
 
        

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号