checkcode.x页面其实什么也不用写
 
<%@ page language=”c#”  codefile=”checkcode.aspx.cs” inherits=”validatecode” %>

<!doctype html public “-//w3c//dtd xhtml 1.1//en” “http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd”>
<html xmlns=””>
<head runat=”server”>
    <title>untitled page</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
      
    </form>
</body>
</html>
 
checkcode.aspx.cs页面代码,生成验证码的主要部分
 
using system;
using system.data;
using system.configuration;
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 validatecode : system.web.ui.page
 {
     private void page_load(object sender, system.eventargs e)
     {
         this.createcheckcodeimage(generatecheckcode());
     }
     #region web 窗体设计器生成的代码
     override protected void oninit(eventargs e)
     {
         //
         // codegen: 该调用是 asp.net web 窗体设计器所必需的。
         //
         initializecomponent();
         base.oninit(e);
     }
 
      /// <summary>
      /// 设计器支持所需的方法 – 不要使用代码编辑器修改
      /// 此方法的内容。
      /// </summary>
     private void initializecomponent()
     {
         this.load += new system.eventhandler(this.page_load);
     }
     #endregion

     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();

             if (number % 2 == 0)
                 code = (char)(‘0’ + (char)(number % 10));
             else
                 code = (char)(‘a’ + (char)(number % 26));

             checkcode += code.tostring();
         }

         session[“checkcodeimage”] = 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:label id=”labflag” runat=”server” text=”label”></asp:label>
                       <asp:textbox id=”txtyz_code” runat=”server” maxlength=”5″ width=”55px” bordercolor=”gray” borderwidth=”1px”></asp:textbox>
                       <img title=”看不清点击换一张” align=”absmiddle” onclick=”this.src=this.src+’?'” src=”password/checkcode.aspx” />
 
后台判断
        if (string.compare(session[“checkcodeimage”].tostring(), this.txtyz_code.text.trim().toupper(), true) != 0)
        {
            this.labflag.text = “验证码错误,请输入正确的验证码。”;
        }
 

 

摘自 爱智旮旯