zoukankan      html  css  js  c++  java
  • 验证码

    <%@ WebHandler Language="C#" Class="show" %>
    
    using System;
    using System.Web;
    using System.Drawing;
    using System.Web.SessionState;//IRequiresSessionState的命名空间
    
    public class show : IHttpHandler, IRequiresSessionState
    {
        
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "image/jpeg";//要输出的类型
            
            //写代码
            //造一张图出来
            Bitmap img = new Bitmap(50, 20);//造空白图
            Graphics gr = Graphics.FromImage(img);//往哪个图上去绘制
            Font font = new Font("宋体", 12, FontStyle.Bold);//设置字体
            SolidBrush brush = new SolidBrush(Color.White);//设置刷子
            gr.FillRectangle(brush, 0, 0, 50, 20);//刷子绘制的形状
            brush.Color = Color.Red;//颜色
    
            //往图上画文字,先生成四位验证码
            string s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string str = "";
            Random rand = new Random();//初始化随机数
            int[] arr = new int[4];
            for (int i = 0; i < 4; i++)
            {
                arr[i] = rand.Next(62);
                int count = 0;
                if (i > 0)
                {
                    for (int j = 0; j < i; j++)
                    {
                        if (arr[i] == arr[j])
                        {
                            count++;
                        } 
                    }
                }
    
                if (count > 0)
                {
                    i--;
                    continue;
                }
                str += s.Substring(arr[i], 1).ToString();
            }
            
            context.Session["yanzheng"] = str;//存到session
    
            gr.DrawString(str, font, brush, 0, 0);//画内容
    
            img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
    
    }
  • 相关阅读:
    CentOS7源码安装Nginx
    CentOS7 源码安装PHP
    Linux系统终端命令提示符设置
    蓝绿发布方案
    datatable 列名重新排序
    C#后台解析 json 动态解析 通用(Dictionary)
    调用webservice 的时候没法输入参数
    soapui 调用wsdl 步骤以及出现的问题
    多文件压缩
    老毛桃+360急救箱结合(转)
  • 原文地址:https://www.cnblogs.com/hansonglin/p/5202882.html
Copyright © 2011-2022 走看看