zoukankan      html  css  js  c++  java
  • 简单的验证码控件

    不是很漂亮,只是用于研究其原理;

    是新建了一个ashx文件(一般处理程序,因为我还在学习中……)

    在aspx页中的form表单中添加调用;

     <img src="验证码实例.ashx" alt="" />

    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
    {
        
    /// <summary>
        
    /// 验证码实例 的摘要说明
        
    /// </summary>
        public class 验证码实例 : IHttpHandler, IRequiresSessionState   //在一般处理程序中使用Session要实现该接口,在System.Web.SessionState中;
        {

            
    public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType 
    = "image/JPEG";    //返回jpg类型;
                using (Bitmap bitmap = new Bitmap(14080))  //像素大小;
                {
                    
    using (Graphics g = Graphics.FromImage(bitmap)) //生成一个画板
                    {

                        Random rand 
    = new Random();
                        
    int code = rand.Next(1000999999);   //制定随机函数,用于限定字随机字符串大小;
                        string strCode = code.ToString();
                        HttpContext.Current.Session[
    "Code"= strCode;   //在一般处理程序中使用Session接口;
                        g.Clear(Color.YellowGreen); //指定背景颜色;
                        g.DrawString(strCode, new Font("微输雅黑"20), Brushes.White, new PointF(1525));  //画的图片相关参数,(字体,大小),颜色,位置;
                        bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);       //输出到流中并保存为jpg格式;
                    }
                }
            }

            
    public bool IsReusable
            {
                
    get
                {
                    
    return false;
                }
            }
        }
    }
  • 相关阅读:
    【转载】MongoDB 数据库的备份与恢复
    【转载】Vim命令合集
    【转载】Mac 让 iTerm2 记住用户名密码
    CSS 实现单行、多行文本溢出显示省略号
    【转载】如何在Vue2中实现组件props双向绑定
    JavaScript 获取当日在今年第几周
    CentOS 7 安装配置FTP服务器(vsftpd)
    CentOS 7 防火墙(firewall)常用命令
    Vs Code 之 实现右键打开文件夹
    git 报错
  • 原文地址:https://www.cnblogs.com/zhuiyi/p/2034874.html
Copyright © 2011-2022 走看看