asp.net2.0生成验证码

来源:互联网 发布:网站源码下载 php 编辑:程序博客网 时间:2024/05/21 17:01
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);
    }

}