关闭

ASP.NET随机生成验证码图片

197人阅读 评论(0) 收藏 举报

using System;
using System.Web;
using System.Drawing;
/// <summary>
/// 验证码生成
/// </summary>
public partial class util_VerifyCode : System.Web.UI.Page
{
    //验证码有效字符
    string codeSerial = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
    /// <summary>
    /// 创建随机码字符串
    /// </summary>
    /// <param name="codeLen"></param>
    /// <returns></returns>
    private string CreateVerifyCode(int codeLen)
    {
        string[] arr = this.codeSerial.Split(',');

        string code = "";

        int randValue = -1;

        Random rand = new Random();

        for (int i = 0; i < codeLen; i++)
        {
            randValue = rand.Next(0, arr.Length - 1);

            code += arr[randValue];
        }
        return code;
    }
    /// <summary>
    /// 创建验证码图片
    /// </summary>
    /// <param name="code">随机码</param>
    /// <returns></returns>
    public Bitmap CreateImageCode(string code)
    {
        Color backgroundColor = Color.White;  //图片背景色
        int padding = 2;    //边框补(默认1像素)
        int fontSize = 10;  //字体大小
        string fontName = "Arial"   ;  //字体

        int fWidth = fontSize + padding;   //字体宽
        int imageWidth = (int)(code.Length * fWidth) + 4 + padding*2;   //图片宽度
        int imageHeight = fontSize * 2 + 2;                    //图片高度

        System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);  //图片对象
        Graphics g = Graphics.FromImage(image);
        g.Clear(backgroundColor);  //刷底色


        int left = 1, top = 2;
        Font f=null;
        Brush b=null;

        //随机字体和颜色的验证码字符
        for (int i = 0; i < code.Length; i++)
        {
            f = new System.Drawing.Font(fontName, fontSize, System.Drawing.FontStyle.Bold);  //字体
            b = new System.Drawing.SolidBrush(Color.Black);  //黑色字体

            g.DrawString(code[i].ToString(), f, b, left, top);
            left += fWidth;
        }

        //画一个边框 边框颜色为Color.Gainsboro
        g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1);
        g.Dispose();

        return image;
    }
    /// <summary>
    /// 直接输出图形到页面
    /// </summary>
    /// <param name="code"></param>
    /// <param name="context"></param>
    private void CreateImageOnPage(string code, HttpContext context)
    {
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        Bitmap image = this.CreateImageCode(code);
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

        context.Response.ClearContent();
        context.Response.ContentType = "image/Jpeg";
        context.Response.BinaryWrite(ms.GetBuffer());

        ms.Close();
        ms = null;
        image.Dispose();
        image = null;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string code=CreateVerifyCode(4);  //生成随机码
        Session[  "VerifyCode"  ] = code;   //随机码保存
        CreateImageOnPage(code, this.Context);  //生成图片
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3415次
    • 积分:100
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档