using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Drawing;
using System.Web.SessionState; namespace MT
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class verCode : IHttpHandler, IRequiresSessionState
{
private Random RandomSeed = new Random(); public void ProcessRequest(HttpContext context)
{ // 生成随机数 iIlLoO0
string strWord = "23456789QWERTYUPASDFGHKXCVBNM"; string NumStr = null;
for (int i = ; i < ; i++)
{
NumStr += strWord[RandomSeed.Next(, strWord.Length)];
} //将验证码保存到session中
context.Session["vcode"] = NumStr.ToLower(); CreateImages(context, NumStr);
} #region 生成验证码图片
private void CreateImages(HttpContext context,string checkCode)
{
int iwidth = (int)(checkCode.Length * );
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, );
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//定义字体
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
Random rand = new Random();
//随机输出噪点
for (int i = ; i < ; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawRectangle(new Pen(Color.LightGray, ), x, y, , );
}
//输出不同字体和颜色的验证码字符
for (int i = ; i < checkCode.Length; i++)
{
int cindex = rand.Next();
int findex = rand.Next(); Font f = new System.Drawing.Font(font[findex], , System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii = ;
if ((i + ) % == )
{
ii = ;
}
g.DrawString(checkCode.Substring(i, ), f, b, + (i * ), ii);
}
//画一个边框
g.DrawRectangle(new Pen(ColorTranslator.FromHtml("#CCCCCC"), ), , , image.Width - , image.Height - ); //输出到浏览器
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.ClearContent();
//Response.ClearContent();
context.Response.ContentType = "image/gif";
context.Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose(); }
#endregion public bool IsReusable
{
get
{
return false;
}
}
}
}

C# 一般处理程序生成验证码的更多相关文章

  1. .NET使用一般处理程序生成验证码

    运行的效果图: HTML的代码: <head> <script type="text/javascript"> function changeCode() ...

  2. 在ASP.NET中使用一般处理程序生成验证码

    如果期望一般处理程序(ashx)处理Session,必须实现[System.Web.SessionState]命名空间下的[IRequiresSessionState]接口. asp.net中的验证码 ...

  3. 以前积攒的一个用Java程序生成验证码的代码

    一个Java类: package com.abc.ufo.util; import java.awt.Color; import java.awt.Font; import java.awt.Grap ...

  4. asp.net中ashx生成验证码代码放在Linux(centos)主机上访问时无法显示问题

    最近有个项目加入了验证码功能,就从自己博客以前的代码中找到直接使用,直接访问验证码页面报错如下: 源代码:asp.net中使用一般处理程序生成验证码 Application Exception Sys ...

  5. asp.net 一般处理程序实现网站验证码

    使用VerifyCode.ashx一般处理程序生成验证码,实现如下: using System; using System.Drawing; using System.Web; using Syste ...

  6. Asp.net 处理程序(第五篇)

    HttpApplication有19个标准事件,当到达第8个事件PostMapRequestHandler触发的时候,标志着已经获取到了处理请求的处理程序对象,在第11个事件PreRequestHan ...

  7. 【python】列出http://www.cnblogs.com/xiandedanteng中所有博文的标题

    代码: # 列出http://www.cnblogs.com/xiandedanteng中所有博文的标题 from bs4 import BeautifulSoup import requests u ...

  8. Node.js 网页爬虫再进阶,cheerio助力

    任务还是读取博文标题. 读取app2.js // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // cheerio模块, ...

  9. Node.js 网页瘸腿稍强点爬虫再体验

    这回爬虫走得好点了,每次正常读取文章数目总是一样的,但是有程序僵住了情况,不知什么原因. 代码如下: // 内置http模块,提供了http服务器和客户端功能 var http=require(&qu ...

随机推荐

  1. 给kali linux2.0装一个中文输入法

    没有中文输入法好痛苦啊.. 毕竟做了无限网卡,虚拟机和主机可以完完全全当两台设备使用了,所以kali还是需要一个中文输入法才方便. 由于使用的是比较新的kali版本和源,现在安装fcitx已经可以直接 ...

  2. python 模块之-hashlib

    python 模块hashlib import hashlib m=hashlib.md5()         # 生成MD5加密对象 m.update('jiami-string'.encode(' ...

  3. ceph 搭建nginx负载3个对象网关

    nginx.conf user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid ...

  4. BZOJ1563 NOI2009诗人小G(动态规划+决策单调性)

    设f[i]为前i行的最小不协调度,转移枚举这一行从哪开始,显然有f[i]=min{f[j]+abs(s[i]-s[j]+i-j-1-m)p}.大胆猜想有决策单调性就好了.证明看起来很麻烦,从略.注意需 ...

  5. BZOJ3771 Triple(FFT+容斥原理)

    思路比较直观.设A(x)=Σxai.先把只选一种的统计进去.然后考虑选两种,这个直接A(x)自己卷起来就好了,要去掉选同一种的情况然后除以2.现在得到了选两种的每种权值的方案数,再把这个卷上A(x). ...

  6. BZOJ2173 整数的lqp拆分(生成函数)

    首先有序整数拆分有个显然的递推式是g(n)=Σg(i) (i=0~n-1),即枚举加入最后一个数之前和是多少.(虽然不用递推式也能显然地知道答案是2n-1). 类似地,lqp拆分有递推式f(n)=Σf ...

  7. Spring AOP动态代理原理与实现方式

    AOP:面向切面.面向方面.面向接口是一种横切技术横切技术运用:1.事务管理: (1)数据库事务:(2)编程事务(3)声明事物:Spring AOP-->声明事物   2.日志处理:3.安全验证 ...

  8. day23 模块引入的一些说明

    模块导入多次也是只导入一次 sys.modules里面会查看有没有被导入 导入后的模块内部的函数,变量就都可以拿来用了 给模块起别名,可以提高代码的兼容性 import time as t 但是被起别 ...

  9. day9 函数练习题

    写代码,接受n个数字,求这些数字的和 def sum_func(*args): total = 0 for i in args: total+=i return total print(sum_fun ...

  10. (python爬取小故事网并写入mysql)

    前言: 这是一篇来自整理EVERNOTE的笔记所产生的小博客,实现功能主要为用广度优先算法爬取小故事网,爬满100个链接并写入mysql,虽然CS作为双学位已经修习了三年多了,但不仅理论知识一般,动手 ...