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. Java动态代理、XML、正则

    15.1  动态代理        在之后学习Spring框架时,Spring框架有一大核心思想,就是AOP,(Aspact-Oriented-Programming 面向切面编程) 而AOP的原理就 ...

  2. HDU5399-多校-模拟

    Too Simple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. day9 集合基础命令

    集合的创建 s = set("hello") print(s) s = set({","alex","sb"}) print(s) ...

  4. idea 项目打包发布

    clean install -Dmaven.test.skip=true -pl 项目名(maven为准) -am -amd

  5. MT【71】数列裂项放缩题

    已知${a_n}$满足$a_1=1,a_{n+1}=(1+\frac{1}{n^2+n})a_n.$证明:当$n\in N^+$时, $(1)a_{n+1}>a_n.(2)\frac{2n}{n ...

  6. 自学Linux Shell3.6-文件查看命令file cat more less tail head

    点击返回 自学Linux命令行与Shell脚本之路 3.6-文件查看命令file cat more less tail head 1.参看文件类型file 该命令用来识别文件类型,也可用来辨别一些文件 ...

  7. 自学Linux Shell4.2-监测磁盘空间mount umount df du

    点击返回 自学Linux命令行与Shell脚本之路 4.2-监测磁盘空间mount umount  df du 1. 挂载存储媒体mount  移除存储媒体umount ls命令用于显示文件目录列表, ...

  8. 【ARC063E】Integers on a tree

    Description 给定一棵\(n\)个点的树,其中若干个点的权值已经给出.现在请为剩余点填入一个值,使得相邻两个点的差的绝对值恰好为1.请判断能否实现,如果能,请将方案一并输出. Solutio ...

  9. bzoj1492/luogu4027 货币兑换 (斜率优化+cdq分治)

    设f[i]是第i天能获得的最大钱数,那么 f[i]=max{在第j天用f[j]的钱买,然后在第i天卖得到的钱,f[i-1]} 然后解一解方程什么的,设$x[j]=\frac{F[j]}{A[j]*Ra ...

  10. mysql数据库几种引擎

    · InnoDB:用于事务处理应用程序,具有众多特性,包括ACID事务支持.(提供行级锁) · BDB:可替代InnoDB的事务引擎,支持COMMIT.ROLLBACK和其他事务特性. · Memor ...