asp.net 验证码控件

作者:袖梨 2022-06-25

asp教程.net 验证码控件
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.drawing;
using system.drawing.imaging;
using system.web.sessionstate;

namespace webapp
{
    ///


    /// 验证码实例 的摘要说明
    ///

    public class 验证码实例 : ihttphandler, irequiressessionstate   //在一般处理程序中使用session要实现该接口,在system.web.sessionstate中;
    {

        public void processrequest(httpcontext context)
        {
            context.response.contenttype = "image/jpeg";    //返回jpg类型;
            using (bitmap bitmap = new bitmap(140, 80))  //像素大小;
            {
                using (graphics g = graphics.fromimage(bitmap)) //生成一个画板
                {

                    random rand = new random();
                    int code = rand.next(1000, 999999);   //制定随机函数,用于限定字随机字符串大小;
                    string strcode = code.tostring();
                    httpcontext.current.session["code"] = strcode;   //在一般处理程序中使用session接口;
                    g.clear(color.yellowgreen); //指定背景颜色;
                    g.drawstring(strcode, new font("微输雅黑", 20), brushes.white, new pointf(15, 25));  //画的图片相关参数,(字体,大小),颜色,位置;
                    bitmap.save(context.response.outputstream, imageformat.jpeg);       //输出到流中并保存为jpg格式;
                }
            }
        }

        public bool isreusable
        {
            get
            {
                return false;
            }
        }
    }
}

相关文章

精彩推荐