AdolphYang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

验证码精简版:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Web_Cassini.Day6
{
    /// <summary>
    /// generateValidCode 的摘要说明
    /// </summary>
    public class generateValidCode : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            Random ran = new Random();
            int num = ran.Next(1000, 10000);
            context.Session[MyConstClass.LOGINVALIDCODE] = num.ToString();

            using(Bitmap map=new Bitmap(60,30))
            {
                using(Graphics g=Graphics.FromImage(map))
                using(Font font=new Font(FontFamily.GenericSerif,15))
                {
                    g.DrawString(num.ToString(), font, Brushes.Red, 0, 0);
                }
                map.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
验证码精简版

 

posted on 2015-09-13 06:30  AdolphYang  阅读(149)  评论(0编辑  收藏  举报