Asp.net生成图片验证码类

80酷酷网    80kuku.com

  asp.net|验证码

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;
using System.Drawing.Imaging;
using System.Drawing.Text;
public class RandPic:System.Web.UI.Page
{
    /// <summary>
    /// 验证码数字
    /// </summary>
    public int Num = 0;
    /// <summary>
    /// 图片宽度
    /// </summary>
    public int ImgWidth=60;
    /// <summary>
    /// 图片高度
    /// </summary>
    public int ImgHeight=30;
    public RandPic()
    {
    }
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="RndNum">验证码数字</param>
    public RandPic(int RndNum)
    {
        if (RndNum > 0)
        {
            Num = RndNum;
        }
    }
    /// <summary>
    /// 生成图片
    /// </summary>
    public void Make()
    {
        Bitmap map=new Bitmap(ImgWidth,ImgHeight);
        Graphics gp = Graphics.FromImage(map);
        gp.Clear(Color.White);
        Font ft = new Font("Arial Black", 13);
        PointF point =new PointF(3,3);
        gp.DrawString(Num.ToString(), ft, Brushes.BurlyWood, point);
        gp.Save();
        map.Save(Response.OutputStream, ImageFormat.Gif);
    }
}
调用页 showRand.aspx
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 _14Base;
public partial class Manager_LogRnd : RandPic
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Num = 8888;
        HttpCookie cookie = new HttpCookie("UserRnd", rnd.ToString());
        Response.Cookies.Add(cookie);
        this.Make();
    }
}

 

 

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: