asp.net2.0生成验证码,并显示验证码

1、建立网站,添加一个网页ValitionNo.aspx,和Default.aspx,新建一个文件夹images。如图所示:

2、ValitionNo.aspx里不用放任何控件,代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValitionNo.aspx.cs" Inherits="guestBook_ValitionNo" %>

<!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" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
    
</div>
    
</form>
</body>
</html>


  双击页面ValitionNo.aspx进入ValitionNo.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.Imaging;
using
 System.Drawing.Drawing2D;
using
 System.Drawing;

public partial class
 guestBook_ValitionNo : System.Web.UI.Page
{
    
protected void Page_Load(object
 sender, EventArgs e)
    {

        Random rd 
= new Random(); //创建随机数对象

        int valationNo = 1000 + rd.Next(8999);//取得4位随机数

        
//建立位图对象

        Bitmap newBitmap = new Bitmap(3416, PixelFormat.Format32bppArgb);
        
//根据上面创建的位图对象创建绘图面

        Graphics g = Graphics.FromImage(newBitmap);
        
//以指定的颜色填充矩形区

        g.FillRectangle(new SolidBrush(Color.White), new Rectangle(003416));
        
        
//创建字体对象

        Font textFont = new Font("Times New Roman"10);

        
//创建RectangleF结构指定一个区域

        RectangleF rectangle = new RectangleF(603416);

        
//使用指定的颜色填充上面RectangleF结构指定的矩形区域

        g.FillRectangle(new SolidBrush(Color.BurlyWood), rectangle);
        
//在上面填充的矩形区域中填充上面生成的随机数

        g.DrawString(valationNo.ToString(), textFont, new SolidBrush(Color.Blue), rectangle);
        
//把创建的位图保存到指定的路径

        newBitmap.Save(Server.MapPath("images"+ //VaImg.gif, ImageFormat.Gif);
        newBitmap.Save(Response.OutputStream, ImageFormat.Gif);
    }
}


3、在Default.aspx里添加一个图片按钮和普通按钮,ImageButton的图片路径是~/ValitionNo.aspx,而不是某张图片。代码如下:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        验证码:
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/ValitionNo.aspx" />
        
<asp:Button ID="Button1" runat="server" Text="换一张" /></div>
    
</form>
</body>
</html>

在Default.aspx.cs里不用添任何额外代码。Default.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;

public partial class
 _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object
 sender, EventArgs e)
    {
        
    }
}

4、运行效果如下所示:

如果点验证码图片或者点换一张按钮,都可以使验证码的数字随机改变。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值