转载请注明出处:http://www.cnblogs.com/wu-jian
转载请注明出处:http://www.cnblogs.com/wu-jian
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace WuJian
{
/// <summary>
/// 【GET参数】
/// length : 验证码字符位数
/// clarity : 图片清晰度,0-100
/// width : 图片宽度 16-480
/// height : 图片高度 16-320
/// fontfamily : 字体名称
/// fontsize : 字体大小 8-72
/// fontstyle : 字体样式 1 粗体 2 斜体 3 粗斜体
/// x : 验证码绘制起始坐标 X
/// y : 验证码绘制起始坐标 Y
///
/// 【产生COOKIE】
/// 结果:Cookie["ImageCode"]["ImageCode"]
/// </summary>
public partial class ImageCode : System.Web.UI.Page
{
// 验证码长度
private int length = 5;
// 图片清晰度
private int clarity = 80;
// 图片宽度
private int width = 62;
// 图片高度
private int height = 18;
// 字体家族名称
private string fontfamily = "Arial";
// 字体大小
private int fontsize = 14;
// 字体样式
private int fontstyle = 2;
// 绘制起始坐标 X
private int x = 0;
// 绘制起始坐标 Y
private int y = 0;
protected void Page_Load(object sender, EventArgs e)
{
#region 读取 Request 传递参数
// 获取代码长度设置
if (Request["length"] != null)
{
try
{
length = Int32.Parse(Request["length"]);
// 规定验证码长度
if (length < 4 || length > 16)
throw new System.Exception("验证码长度必须在4到16之间");
}
catch (System.Exception error)
{
throw error;
}
}
// 获取图片清晰度设置
if (Request["clarity"] != null)
{
try
{
clarity = Int32.Parse(Request["clarity"]);
// 验证清晰度
if (clarity < 0 || clarity > 100)
throw new System.Exception("图片清晰度必须在0到100之间");
}
catch (System.Exception error)
{
throw error;
}
}
// 获取图片宽度
if (Request["width"] != null)
{
try
{
width = Int32.Parse(Request["width"]);
if (width < 16 || width > 480)
throw new System.Exception("图片宽度必须在16到480之间");
}
catch (System.Exception error)
{
throw error;
}
}
// 获取图片高度
if (Request["height"] != null)
{
try
{
height = Int32.Parse(Request["height"]);
if (height < 16 || height > 320)
throw new System.Exception("图片高度必须在16到320之间");
}
catch (System.Exception error)
{
throw error;
}
}
// 获取验证码字体家族名称
if (Request["fontfamily"] != null)
{
fontfamily = Request["fontfamily"];
}
// 获取验证码字体大小
if (Request["fontsize"] != null)
{
try
{
fontsize = Int32.Parse(Request["fontsize"]);
if (fontsize < 8 || fontsize > 72)
throw new System.Exception("字体大小必须在8到72之间");
}
catch (System.Exception error)
{
throw error;
}
}
// 获取字体样式
if (Request["fontstyle"] != null)
{
try
{
fontstyle = Int32.Parse(Request["fontstyle"]);
}
catch (System.Exception error)
{
throw error;
}
}
// 验证码绘制起始位置 X
if (Request["x"] != null)
{
try
{
x = Int32.Parse(Request["x"]);
}
catch (System.Exception error)
{
throw error;
}
}
// 验证码绘制起始位置 Y
if (Request["y"] != null)
{
try
{
y = Int32.Parse(Request["y"]);
}
catch (System.Exception error)
{
throw error;
}
}
#endregion
string validateCode = this.createValidateCode();
// 生成BITMAP图像
Bitmap bitmap = new Bitmap(width, height);
// 给图像设置干扰
this.disturbBitmap(bitmap);
// 绘制验证码图像
this.drawValidateCode(bitmap, validateCode);
// 保存验证码图像,等待输出
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
}
#region 随机生成验证码并生成Cookie,返回验证码字符
private string createValidateCode()
{
string result = "";
string include = "0123456789";
Random random = new Random();
for (int i = 0; i < this.length; i++)
{
result += include[random.Next(0, 9)].ToString();
}
//生成Cookie
HttpCookie myCookie = new HttpCookie("ImageCode");
myCookie.Values.Add("ImageCode", result);
Response.AppendCookie(myCookie);
return result;
}
#endregion
#region 为图片设置干扰点
private void disturbBitmap(Bitmap bitmap)
{
// 通过随机数生成
Random random = new Random();
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
if (random.Next(100) <= this.clarity)
bitmap.SetPixel(i, j, Color.White);
}
}
}
#endregion
#region 绘制验证码图片
private void drawValidateCode(Bitmap bitmap, string validateCode)
{
// 获取绘制器对象
Graphics g = Graphics.FromImage(bitmap);
// 设置绘制字体
Font font = new Font(fontfamily, fontsize, this.getFontStyle());
// 绘制验证码图像
g.DrawString(validateCode, font, Brushes.Black, x, y);
}
#endregion
#region 换算验证码字体样式:1 粗体 2 斜体 3 粗斜体,默认为普通字体
private FontStyle getFontStyle()
{
if (fontstyle == 1)
return FontStyle.Bold;
else if (fontstyle == 2)
return FontStyle.Italic;
else if (fontstyle == 3)
return FontStyle.Bold | FontStyle.Italic;
else
return FontStyle.Regular;
}
#endregion
}//end class
}