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. BZOJ4699 树上的最短路(最短路径+dfs序+线段树+堆+并查集)

    首先一般化的将下水道和塌陷看成一个东西.注意到在从源点出发的所有需要使用某条下水道的最短路径中,该下水道只会被使用一次,该下水道第一个被访问的点相同,且只会在第一个访问的点使用该下水道.这个第一个访问 ...

  2. hdu 4897 Little Devil I (树链剖分+线段树)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意: 给你一棵树,一开始每条边都是白色,有三种操作: 1.将 u - v路径上的边转换颜色 ...

  3. UVa - 10341

    Solve the equation:p ∗ e ^−x + q ∗ sin(x) + r ∗ cos(x) + s ∗ tan(x) + t ∗ x ^2 + u = 02 + u = 0where ...

  4. OCIEnvCreate 失败,返回代码为 -1,但错误消息文本不可用

    解决方案:oracle 版本太低,请装11G或以上版本..

  5. Huawei运维记录

    Huawei运维记录 01 Huawei运维记录-AC6005-8AP设备启动界面 02 Huawei运维记录-AC6005-8AP添加授权码 03 Huawei运维记录-AC6005版本升级步骤

  6. 【BZOJ3668】【NOI2014】起床困难综合症(贪心)

    [NOI2014]起床困难综合症(贪心) 题面 Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚 ...

  7. Haproxy Mysql cluster 高可用Mysql集群

    -----client-----------haproxy---------mysql1----------mysql2------192.168.1.250 192.168.1.1 192.168. ...

  8. Azure HDInsight 上的 Spark 群集配合自定义的Python来分析网站日志

    一.前言:本文是个实践博客,演示如何结合使用自定义库和 HDInsight 上的 Spark 来分析日志数据. 我们使用的自定义库是一个名为 iislogparser.py的 Python 库. 每步 ...

  9. CF986C AND Graph

    半年前做的一道题现在还是不会 x&y=0 意味着,x的补集的子集都是和x直接相连的 不妨令图中的点数就是2^n 那么可以直接从x^((1<<n)-1)开始记忆化爆搜,路上遇到的都是 ...

  10. mysql常见问题解决

    日常使用mysql数据库遇到的一些问题,做下记录,会持续更新. 一.MySql Host is blocked because of many connection errors; unblock w ...