.net一般处理程序ashx生成验证码(原创+转载)

//第一种

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

namespace sizhengwang.BLL
{
    /// <summary>
    /// GetValImage 的摘要说明
    /// </summary>
    public class GetValImage : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            using (Bitmap bitmap = new Bitmap(80, 20))//创建图片
            {
                using (Graphics gr = Graphics.FromImage(bitmap))//创建图片的画布
                {
                    Random rand = new Random();
                    string code = yzm(4);
                    gr.Clear(Color.White);
                    gr.DrawString(" " + code, new Font("宋体", 14), Brushes.Blue, new PointF(0, 0));

                    bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg); //返回一个jpeg图片对象
                    HttpContext.Current.Session["code"] = code.ToLower(); //保存到session
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public static string yzm(int len) //生成验证码
        {
            string str = "0,1,2,3,4,5,6,7,8,9,";
            str += "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";
            str += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
            string[] s = str.Split(new char[] { ',' });
            string strnum = "";
            int tag = -1;
            Random rnd = new Random();
           
            for (int i = 1; i <= len; i++)
            {
                if (tag == -1)
                {
                    rnd = new Random(i * tag * unchecked((int)DateTime.Now.Ticks));
   

转载于:https://www.cnblogs.com/pukuimin/archive/2012/06/23/2988152.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值