asp.net2.0生成验证码

本文介绍了一种使用C#生成四位数字验证码图片的方法。通过创建随机数并将其绘制在自定义大小和格式的位图上,实现了验证码图片的生成。具体步骤包括生成随机数、设置位图属性、填充背景和文字等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值