using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
namespace HRManager.CommonComponent
{
/// <summary>
/// 生成验证码图象类
/// </summary>
public class ValidateCode
{
private const int CODELEN = 4; //4位验证码
public string code; //生成的验证码
public string imgFilePath ; //生成的图象路径
public string imgFileName; //生成的图象文件名
/// <summary>
/// 构造函数,得到保存图象的路径
/// </summary>
public ValidateCode(string imgFilePath)
{
this.imgFilePath = imgFilePath;
}
/// <summary>
/// 析构函数,删除生成的图象文件
/// </summary>
~ValidateCode()
{
File.Delete(this.imgFilePath+this.imgFileName); //使用File的Delete静态方法,删除临时图象
}
/// <summary>
/// 生成验证码图象文件,并存储在服务器中
/// </summary>
public void GenerateCodeImage()
{
this.GetCode();
Bitmap img = null;
Graphics g = null;
int gHeight = code.Length * 12;
img = new Bitmap(gHeight, 25);
g = Graphics.FromImage(img);
g.Clear(Color.AliceBlue); //背景颜色
Font font = new Font("Arial Black", 10); //文字字体
SolidBrush brush = new SolidBrush(Color.Black); //文字颜色
g.DrawString(this.code, font, brush, 3, 3);
this.imgFileName = this.code + ".Png";
img.Save(this.imgFilePath+this.imgFileName, ImageFormat.Png);
g.Dispose();
img.Dispose();
}
/// <summary>
/// 生成随即的四位字母、数字混合串
/// </summary>
/// <returns>四位字母、数字混合串</returns>
public void GetCode()
{
char[] allChars = new char[]{
'0','1','2','3','4','5','6','7','8','9',
'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',
'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'
};
Random rand = new Random();
for (int i = 0; i < CODELEN; i++)
{
rand = new Random((i+1) * (int)DateTime.Now.Ticks);
this.code+= allChars[rand.Next(allChars.Length - 1)];
}
}
}
}
--------------------------------
应用
//“验证码”,放在隐藏文本框TextBoxValiCode2中
ValidateCode vcode = new ValidateCode(Request.PhysicalApplicationPath + "");
vcode.GenerateCodeImage();
ImageValiCode.ImageUrl = vcode.imgFileName;
TextBoxValiCode2.Text = vcode.code;
----------------------------------------方法二 参考 未验证
验证码实现方法
protected void Page_Load(object sender, EventArgs e)
{
string checkCode = CreateRandomCode(4);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
}
private string CreateRandomCode(int codeCount)
{
// 函数功能:产生数字和字符混合的随机字符串
string allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] allCharArray = allChar.ToCharArray();
string randomCode = "";
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
int r=rand.Next(61);
randomCode+=allCharArray.GetValue(r);
}
return randomCode;
}
private void CreateImage(string checkCode)
{
// 生成图象验证码函数
int iwidth = (int)(checkCode.Length * 11.5);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(Color.Azure);//字母白色
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.Brown);//背景灰色
g.DrawString(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(Color.Black, 0);
Random rand = new Random();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}