//第一种
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));
}
int rndnum = rnd.Next(61);
//if (tag != -1 && tag == rndnum)
//{
// return yzm(1);
//}
tag = rndnum;
strnum += s[rndnum];
}
return strnum;
}
}
}
//第二种(转载)
<%@ WebHandler Language="C#" Class="ValidateCode" %>
using System;
using System.Web;
using System.Drawing;
using System.Web.SessionState;
public class ValidateCode : IHttpHandler, IRequiresSessionState
{
HttpContext context;
//在此文件被访问时自动调用 相当于页面的PageLoad事件
public void ProcessRequest (HttpContext context1) {
this.context = context1;
CreateCheckCodeImage(GenerateCheckCode());
}
#region 生成随机字符串
/// <summary>
/// 生成随机字符串
/// </summary>
/// <returns></returns>
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 5; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('0' + (char)(number % 10));
//code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
//Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
//当生成好随机字符串后,将它保存在Session
context.Session["vCode"] = checkCode;//.Add("vCode", checkCode);
return checkCode;
}
#endregion
/// <summary>
/// 根据字符串生成图片
/// </summary>
/// <param name="checkCode">字符串</param>
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//准备缓存流
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//将图片流存入缓存流
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
//清空相应对象内容
context.Response.ClearContent();
//设置相应对象内容格式为图片
context.Response.ContentType = "image/Gif";
//将缓存流中的图片流返回客户端
context.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();//销毁画板
image.Dispose();//销毁图片
}
}
public bool IsReusable {
get {
return false;
}
}
}