随即验证码实现(根据韩现龙代码修改)
protected void Page_Load(object sender, EventArgs e)
{
Encoding gb = Encoding.GetEncoding("gb2312");
//调用函数产生4个随机数字编码
object[] bytes = CreateRegionCode(4);
string str1 = bytes[0].ToString();
string str2 = bytes[1].ToString();
string str3 = bytes[2].ToString();
string str4 = bytes[3].ToString();
//输出的控制台
// Response.Write(str1 + str2 + str3 + str4);
// return;
string srs = str1 + str2 + str3 + str4;
Bitmap srBmp = srBitmap(srs);
System.IO.MemoryStream srMS = new System.IO.MemoryStream();
srBmp.Save(srMS, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(srMS.ToArray());
srBmp.Dispose();
}
/// <summary>
/// 根据指定参数返回BitMap对象
/// 引用如下:
/// using System.Drawing;
/// 调用例子如下:
/// eg1、保存为图象文件为
/// Bitmap srBmp = srBitmap(srs);
/// srBmp.Save(Directory.GetCurrentDirectory() + "\\srs.gif", System.Drawing.Imaging.ImageFormat.Gif);
/// srBmp.Dispose();
/// eg2。网页中调用方式如下
/// Bitmap srBmp = srBitmap(srs);
/// System.IO.MemoryStream srMS = new System.IO.MemoryStream();
/// srBmp.Save(srMS,System.Drawing.Imaging.ImageFormat.Gif);
/// Response.ClearContent();
/// Response.ContentType = "image/gif";
/// Response.BinaryWrite(srMS.ToArray());
/// srBmp.Dispose();
/// </summary>
/// <param name="srs"></param>
/// <returns></returns>
public static Bitmap srBitmap(string srs)
{
//定义图片弯曲的角度
int srseedangle = 40;
//定义图象
Bitmap srBmp = new Bitmap(srs.Length * 20, 30);
//画图
Graphics srGraph = Graphics.FromImage(srBmp);
//清空图象
srGraph.Clear(Color.AliceBlue);
//给图象画边框
srGraph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, srBmp.Width - 1, srBmp.Height - 1);
//定义随即数
Random srRandom = new Random();
//定义画笔
Pen srPen = new Pen(Color.LightBlue, 0);
//画噪点
for (int i = 0; i < 100; i++)
{
srGraph.DrawRectangle(srPen, srRandom.Next(1, srBmp.Width - 2), srRandom.Next(1, srBmp.Height - 2), 1, 1);
}
//将字符串转化为字符数组
char[] srchars = srs.ToCharArray();
//封状文本
StringFormat srFormat = new StringFormat(StringFormatFlags.NoClip);
//设置文本垂直居中
srFormat.Alignment = StringAlignment.Center;
//设置文本水平居中
srFormat.LineAlignment = StringAlignment.Center;
//定义字体颜色
Color[] srColors ={ Color.Black, Color.Red, Color.DarkBlue, Color.Blue, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//定义字体
string[] srFonts ={ "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
//循环画出每个字符
for (int i = 0, j = srchars.Length; i < j; i++)
{
//定义字体 参数分别为字体样式 字体大小 字体字形
Font srFont = new Font(srFonts[srRandom.Next(5)], 12, FontStyle.Regular);
//填充图形
Brush srBrush = new SolidBrush(srColors[srRandom.Next(7)]);
//定义坐标
Point srPoint = new Point(16, 16);
//定义倾斜角度
float srangle = srRandom.Next(-srseedangle, srseedangle);
//倾斜
srGraph.TranslateTransform(srPoint.X, srPoint.Y);
srGraph.RotateTransform(srangle);
//填充字符
srGraph.DrawString(srchars[i].ToString(), srFont, srBrush, 1, 1, srFormat);
//回归正常
srGraph.RotateTransform(-srangle);
srGraph.TranslateTransform(2, -srPoint.Y);
}
srGraph.Dispose();
return srBmp;
}
public static object[] CreateRegionCode(int strlength)
{
string[] rBase = new string[10] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
Random rnd = new Random();
//定义一个object数组用来
object[] bytes = new object[strlength];
for (int i = 0; i < strlength; i++)
{
int r1 = rnd.Next(0, 10);
string str_r1 = rBase[r1].Trim();
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks + i));//更换随机数发生器的种子避免产生重复值
//定义两个字节变量存储产生的随机数字区位码
byte byte1 = Convert.ToByte(str_r1, 16);
//将产生的一个汉字的字节数组放入object数组中
bytes.SetValue(byte1,i);
}
return bytes;
}