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. 软件工程_3rd weeks

    本周上课的第一件事就是四人组队做工程,因为之前没有太多的准备,所以过程有些仓促,只是最后的结果是好的.有了自己的队伍和课题.感觉就应该这样,平时的我们比较随意,没有一丝的紧迫感,这样会督促着我们完成任 ...

  2. HTMl Dom操作

    訪問: getElementById():帶回指定id的所有元素 getElementByTagName():帶回指定標籤名的所有的元素 getElementByClassName():帶回指定類型名 ...

  3. python面对对象编程中会用到的装饰器

    1.property 用途:用来将对像的某个方法伪装成属性来提高代码的统一性. class Goods: #商品类 discount = 0.8 #商品折扣 def __init__(self,nam ...

  4. Python连接字符串用join还是+

    我们先来看一下用join和+连接字符串的例子 str1 = " ".join(["hello", "world"]) str2 = &quo ...

  5. ceph API之PHP的客户端连接

    下载v2的SDK开发包http://pear.amazonwebservices.com/get/sdk-latest.zip 解压到目录下: unzip sdk-latest.zip &&a ...

  6. BZOJ1251序列终结者——非旋转treap

    题目描述 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技 ...

  7. 如何修改可运行Jar包,如何反编译Jar包

    将可运行Jar包,反编译成项目,修改代码,再次编译,打包. 需要工具:jd-gui.myeclipse 具体步骤: 1.使用jd-gui打开原始的Jar包,选择File-->Save All  ...

  8. HNOI2018题解

    在此处输入标题 标签(空格分隔): 未分类 重做了一遍,本来以为很快的,结果搞了一天... 寻宝游戏 可以发现只有\(\&0\)和\(|1\)会对答案有影响 那么对于每一位,我们只要知道最后一 ...

  9. [luogu5008]逛庭院

    首先我们看到数据范围.妈耶!数据这么大,一开始还想用个DP来做,但是看着就不行,那么根据这个数据范围,我们大致可以猜到这道题的算法是一个贪心,那么我们怎么贪呢? 我们首先还是先画一个图: 样例解释一下 ...

  10. 【转】Linux 移动或重命名文件/目录-mv 的10个实用例子

    熟悉了 复制命令,下一个相关的命令就是 mv 命令.当你想要将文件从一个位置移动到另一个地方并且不想复制它,那么mv 命令是完成这个任务的首选. 初识 mv 命令 mv 命令是一个与cp类似的命令,但 ...