验证码
/*
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
*/
//建立位图对象
Bitmap newBitmap = new Bitmap(36, 16, PixelFormat.Format32bppArgb);
//根据上面创建的位图对象创建绘图面
Graphics g = Graphics.FromImage(newBitmap);
//创建字体对象
Font textFont = new Font("Times New Roman", 10);
//创建RectangleF结构指定一个区域
RectangleF rectangle = new RectangleF(0, 0, 36, 16);
//创建随机数对象
Random rd = new Random();
//取得随机数
int valationNo = 1000 + rd.Next(9000);
//使用指定的颜色填充上面RectangleF结构指定的矩形区域
g.FillRectangle(new SolidBrush(Color.BurlyWood), rectangle);
//在上面填充的矩形区域中填充上面生成的随机数
g.DrawString(valationNo.ToString(), textFont, new SolidBrush(Color.Blue), rectangle);
//把创建的位图保存到指定的路径
// 方法一
//newBitmap.Save(Server.MapPath("img") + "\\VaImg.gif", ImageFormat.Gif);
// 方法二
Response.ContentType = "image/jpeg";
newBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
///释放占用的资源
newBitmap.Dispose();
11.中文验证码
/*
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
*/
public static object[] CreateRegionCode(int strlength)
{
string[] rBase=new String [16]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};//定
义一个字符串数组储存汉字编码的组成元素
Random rnd=new Random();
object[] bytes=new object[strlength];//定义一个object数组用来
for(int i=0;i<strlength;i++)
{
//区位码第1位
int r1=rnd.Next(11,14);
string str_r1=rBase[r1].Trim();
//区位码第2位
rnd=new Random(r1*unchecked((int)DateTime.Now.Ticks)+i);//更换随机数发生器的种子避免产生重复
值
int r2;
if (r1==13)
{r2=rnd.Next(0,7);}
else
{r2=rnd.Next(0,16);}
string str_r2=rBase[r2].Trim();
//区位码第3位
rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks)+i);
int r3=rnd.Next(10,16);
string str_r3=rBase[r3].Trim();
//区位码第4位
rnd=new Random(r3*unchecked((int)DateTime.Now.Ticks)+i);
int r4;
if (r3==10)
{r4=rnd.Next(1,16);}
else if (r3==15)
{r4=rnd.Next(0,15);}
else
{r4=rnd.Next(0,16);}
string str_r4=rBase[r4].Trim();
//定义两个字节变量存储产生的随机汉字区位码
byte byte1=Convert.ToByte(str_r1 + str_r2,16);
byte byte2=Convert.ToByte(str_r3 + str_r4,16);
byte[] str_r=new byte[]{byte1,byte2};//将两个字节变量存储在字节数组中
bytes.SetValue(str_r,i);//将产生的一个汉字的字节数组放入object数组中
}
return bytes;
}
private void CreateImage(string checkCode)
{
if(checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 21)),
22);
Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();//生成随机生成器
g.Clear(Color.White);//清空图片背景色
for(int i=0; i<20; 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("楷体", 12, (System.Drawing.FontStyle.Bold));
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);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{g.Dispose();
image.Dispose();}
}
// 在此处放置用户代码以初始化页面
Encoding gb=Encoding.GetEncoding("gb2312");
object[] bytes=CreateRegionCode(4);//调用函数产生4个随机中文汉字编码
//根据汉字编码的字节数组解码出中文汉字
string str1=gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
string str2=gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
string str3=gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
string str4=gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
Session["CheckCode"] = str1 + str2 +str3 +str4;//用于与输入的验证码作对比
CreateImage(str1 + str2 +str3 +str4);
/*
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
*/
//建立位图对象
Bitmap newBitmap = new Bitmap(36, 16, PixelFormat.Format32bppArgb);
//根据上面创建的位图对象创建绘图面
Graphics g = Graphics.FromImage(newBitmap);
//创建字体对象
Font textFont = new Font("Times New Roman", 10);
//创建RectangleF结构指定一个区域
RectangleF rectangle = new RectangleF(0, 0, 36, 16);
//创建随机数对象
Random rd = new Random();
//取得随机数
int valationNo = 1000 + rd.Next(9000);
//使用指定的颜色填充上面RectangleF结构指定的矩形区域
g.FillRectangle(new SolidBrush(Color.BurlyWood), rectangle);
//在上面填充的矩形区域中填充上面生成的随机数
g.DrawString(valationNo.ToString(), textFont, new SolidBrush(Color.Blue), rectangle);
//把创建的位图保存到指定的路径
// 方法一
//newBitmap.Save(Server.MapPath("img") + "\\VaImg.gif", ImageFormat.Gif);
// 方法二
Response.ContentType = "image/jpeg";
newBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
///释放占用的资源
newBitmap.Dispose();
11.中文验证码
/*
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
*/
public static object[] CreateRegionCode(int strlength)
{
string[] rBase=new String [16]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};//定
义一个字符串数组储存汉字编码的组成元素
Random rnd=new Random();
object[] bytes=new object[strlength];//定义一个object数组用来
for(int i=0;i<strlength;i++)
{
//区位码第1位
int r1=rnd.Next(11,14);
string str_r1=rBase[r1].Trim();
//区位码第2位
rnd=new Random(r1*unchecked((int)DateTime.Now.Ticks)+i);//更换随机数发生器的种子避免产生重复
值
int r2;
if (r1==13)
{r2=rnd.Next(0,7);}
else
{r2=rnd.Next(0,16);}
string str_r2=rBase[r2].Trim();
//区位码第3位
rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks)+i);
int r3=rnd.Next(10,16);
string str_r3=rBase[r3].Trim();
//区位码第4位
rnd=new Random(r3*unchecked((int)DateTime.Now.Ticks)+i);
int r4;
if (r3==10)
{r4=rnd.Next(1,16);}
else if (r3==15)
{r4=rnd.Next(0,15);}
else
{r4=rnd.Next(0,16);}
string str_r4=rBase[r4].Trim();
//定义两个字节变量存储产生的随机汉字区位码
byte byte1=Convert.ToByte(str_r1 + str_r2,16);
byte byte2=Convert.ToByte(str_r3 + str_r4,16);
byte[] str_r=new byte[]{byte1,byte2};//将两个字节变量存储在字节数组中
bytes.SetValue(str_r,i);//将产生的一个汉字的字节数组放入object数组中
}
return bytes;
}
private void CreateImage(string checkCode)
{
if(checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 21)),
22);
Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();//生成随机生成器
g.Clear(Color.White);//清空图片背景色
for(int i=0; i<20; 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("楷体", 12, (System.Drawing.FontStyle.Bold));
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);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{g.Dispose();
image.Dispose();}
}
// 在此处放置用户代码以初始化页面
Encoding gb=Encoding.GetEncoding("gb2312");
object[] bytes=CreateRegionCode(4);//调用函数产生4个随机中文汉字编码
//根据汉字编码的字节数组解码出中文汉字
string str1=gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
string str2=gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
string str3=gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
string str4=gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
Session["CheckCode"] = str1 + str2 +str3 +str4;//用于与输入的验证码作对比
CreateImage(str1 + str2 +str3 +str4);