新加一个asp页面(CreateImage.aspx),前端什么也没有,后端只需要如下代码。
用的时候是需要将image的src指向该页面即可;祝后台生成的验证码通过session传递过来,如红色标注部分
public partial class CreateImage : System.Web.UI.Page
{
#region 设置验证码
// 验证码长度
private int codeLen = 4;
// 图片清晰度
private int fineness = 95;
// 图片宽度
private int imgWidth = 80;
// 图片高度
private int imgHeight = 37;
// 字体家族名称
private string fontFamily = "Arial";
// 字体大小
private int fontSize = 15;
// 字体样式
private int fontStyle = 3;
// 绘制起始坐标 X
private int posX = 16;
// 绘制起始坐标 Y
private int posY = 7;
private static char[] allCharArray = new char[] { '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
#endregion
private void Page_Load(object sender, System.EventArgs e)
{
#region 读取 Request 传递参数
// 获取代码长度设置
if (Request["CodeLen"] != null)
{
try
{
codeLen = Int32.Parse(Request["CodeLen"]);
// 规定验证码长度
if (codeLen < 4 || codeLen > 16)
throw new Exception("验证码长度必须在4到16之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片清晰度设置
if (Request["Fineness"] != null)
{
try
{
fineness = Int32.Parse(Request["Fineness"]);
// 验证清晰度
if (fineness < 0 || fineness > 100)
throw new Exception("图片清晰度必须在0到100之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片宽度
if (Request["ImgWidth"] != null)
{
try
{
imgWidth = Int32.Parse(Request["ImgWidth"]);
if (imgWidth < 16 || imgWidth > 480)
throw new Exception("图片宽度必须在16到480之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片高度
if (Request["ImgHeight"] != null)
{
try
{
imgWidth = Int32.Parse(Request["ImgHeight"]);
if (imgWidth < 16 || imgWidth > 320)
throw new Exception("图片高度必须在16到320之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取验证码字体家族名称
if (Request["FontFamily"] != null)
{
fontFamily = Request["FontFamily"];
}
// 获取验证码字体大小
if (Request["FontSize"] != null)
{
try
{
fontSize = Int32.Parse(Request["FontSize"]);
if (fontSize < 8 || fontSize > 72)
throw new Exception("字体大小必须在8到72之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取字体样式
if (Request["FontStyle"] != null)
{
try
{
fontStyle = Int32.Parse(Request["FontStyle"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
// 验证码绘制起始位置 X
if (Request["PosX"] != null)
{
try
{
posX = Int32.Parse(Request["PosX"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
// 验证码绘制起始位置 Y
if (Request["PosY"] != null)
{
try
{
posY = Int32.Parse(Request["PosY"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
#endregion
string validateCode = CreateValidateCode();
// 生成BITMAP图像
Bitmap bitmap = new Bitmap(imgWidth, imgHeight);
// 给图像设置干扰
DisturbBitmap(bitmap);
// 绘制验证码图像
DrawValidateCode(bitmap, validateCode);
bitmap = TwistImage(bitmap,true,4,2);
// 保存验证码图像,等待输出
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
bitmap.Dispose();
}
/// <summary>
/// 随机生成验证码,并把验证码保存到Session中.
/// </summary>
/// <returns></returns>
private string CreateValidateCode()
{
#region 0-9数字的验证码
string randomCode = "";
int t = 0;
Random rand = new Random();
for (int i = 0; i < codeLen; i++)
{
t = rand.Next(allCharArray.Length);
randomCode += allCharArray[t];
}
Session["CheckCode"] = randomCode;
return randomCode;
#endregion
}
/// <summary>
/// 为图片设置干扰点
/// </summary>
/// <param name="bitmap"></param>
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.fineness)
bitmap.SetPixel(i, j, Color.White);
}
}
}
/// <summary>
/// 绘制验证码图片
/// </summary>
/// <param name="bitmap"></param>
/// <param name="validateCode"></param>
private void DrawValidateCode(Bitmap bitmap, string validateCode)
{
// 获取绘制器对象
Graphics g = Graphics.FromImage(bitmap);
// 设置绘制字体
Font font = new Font(fontFamily, fontSize, GetFontStyle());
// 绘制验证码图像
g.DrawString(validateCode, font, Brushes.Black, posX, posY);
font.Dispose();
g.Dispose();
}
/// <summary>
/// 换算验证码字体样式:1 粗体 2 斜体 3 粗斜体,默认为普通字体
/// </summary>
/// <returns></returns>
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;
}
#region 产生波形滤镜效果
private const double PI = 3.1415926535897932384626433832795;
private const double PI2 = 6.283185307179586476925286766559;
/// <summary>
/// 正弦曲线Wave扭曲图片
/// </summary>
/// <param name="srcBmp">图片路径</param>
/// <param name="bXDir">如果扭曲则选择为True</param>
/// <param name="nMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
/// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
/// <returns></returns>
public Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
// 将位图背景填充为白色
System.Drawing.Graphics graph = Graphics.FromImage(destBmp);
graph.FillRectangle(new SolidBrush(Color.White), 0, 0, destBmp.Width, destBmp.Height);
graph.Dispose();
double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
for (int i = 0; i < destBmp.Width; i++)
{
for (int j = 0; j < destBmp.Height; j++)
{
double dx = 0;
dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
dx += dPhase;
double dy = Math.Sin(dx);
// 取得当前点的颜色
int nOldX = 0, nOldY = 0;
nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy * dMultValue);
System.Drawing.Color color = srcBmp.GetPixel(i, j);
if (nOldX >= 0 && nOldX < destBmp.Width
&& nOldY >= 0 && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX, nOldY, color);
}
}
}
return destBmp;
}
#endregion
}