Vildate.aspx.cs代码部分如下:
另建一页面test.aspx
///****************************************************************************************************
/// 在Page_Load方法中调用以下代码段
/// string strKey = "";
/// byte[] data = GenerateVerifyImage(5,ref strKey);
/// Response.OutputStream.Write(data, 0, data.Length);
/// ***************************************************************************************************
/// <summary>
/// 生成图片验证码
/// </summary>
/// <param name="nLen">验证码的长度</param>
/// <param name="strKey">输出参数,验证码的内容</param>
/// <returns>图片字节流</returns>
[System.Web.Services.WebMethod]
public static byte[] GenerateVerifyImage(int nLen, ref string strKey)
{
int nBmpWidth = 13 * nLen + 5;
int nBmpHeight = 25;
System.Drawing.Bitmap bmp = new Bitmap(nBmpWidth, nBmpHeight);
Web web = new Web();
bmp = web.TwistImage(bmp, true, 50, 6);
//1.生成随机背景颜色
int nRed, nGreen, nBlue;//背景的三元色
System.Random rd = new Random((int)System.DateTime.Now.Ticks);
nRed = rd.Next(255) % 128 + 128;
nGreen = rd.Next(255) % 128 + 128;
nBlue = rd.Next(255) % 128 + 128;
//2、填充位图背景
System.Drawing.Graphics graph = Graphics.FromImage(bmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(nRed, nGreen, nBlue)), 0, 0, nBmpWidth, nBmpHeight);
//3、绘制干扰线条,采用比背景约深一些的颜色
int nLines = 3;
System.Drawing.Pen pen = new Pen(Color.FromArgb(nRed - 17, nGreen - 17, nBlue - 17), 2);
for (int a = 0; a < nLines; a++)
{
int x1 = rd.Next() % nBmpWidth;
int y1 = rd.Next() % nBmpHeight;
int x2 = rd.Next() % nBmpWidth;
int y2 = rd.Next() % nBmpHeight;
graph.DrawLine(pen, x1, y1, x2, y2);
}
//采用的字符集,可以随即拓展,并可以控制字符出现的几率
string strCode = "23456789ASDFGHJKZXCVBNMQWERTYUIP";
//4、循环取得字符,并绘制
string strResult = "";
for (int i = 0; i < nLen; i++)
{
int x = (i * 13 + rd.Next(3));
int y = rd.Next(4) + 1;
//确定字体
System.Drawing.Font font = new Font("Courier New", 12 + rd.Next() % 4, FontStyle.Bold);
//随机获取字符
char c = strCode[rd.Next(strCode.Length)];
strResult += c.ToString();
//绘制字符
graph.DrawString(c.ToString(), font, new SolidBrush(System.Drawing.Color.FromArgb(nRed - 60 + y * 3, nGreen - 60 + y * 3, nBlue - 40 + y * 3)), x, y);
}
//5、输出字节流
System.IO.MemoryStream bstream = new MemoryStream();
bmp.Save(bstream, System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Dispose();
graph.Dispose();
strKey = strResult;
byte[] byteReturn = bstream.ToArray();
bstream.Close();
return byteReturn;
}
/// 在Page_Load方法中调用以下代码段
/// string strKey = "";
/// byte[] data = GenerateVerifyImage(5,ref strKey);
/// Response.OutputStream.Write(data, 0, data.Length);
/// ***************************************************************************************************
/// <summary>
/// 生成图片验证码
/// </summary>
/// <param name="nLen">验证码的长度</param>
/// <param name="strKey">输出参数,验证码的内容</param>
/// <returns>图片字节流</returns>
[System.Web.Services.WebMethod]
public static byte[] GenerateVerifyImage(int nLen, ref string strKey)
{
int nBmpWidth = 13 * nLen + 5;
int nBmpHeight = 25;
System.Drawing.Bitmap bmp = new Bitmap(nBmpWidth, nBmpHeight);
Web web = new Web();
bmp = web.TwistImage(bmp, true, 50, 6);
//1.生成随机背景颜色
int nRed, nGreen, nBlue;//背景的三元色
System.Random rd = new Random((int)System.DateTime.Now.Ticks);
nRed = rd.Next(255) % 128 + 128;
nGreen = rd.Next(255) % 128 + 128;
nBlue = rd.Next(255) % 128 + 128;
//2、填充位图背景
System.Drawing.Graphics graph = Graphics.FromImage(bmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(nRed, nGreen, nBlue)), 0, 0, nBmpWidth, nBmpHeight);
//3、绘制干扰线条,采用比背景约深一些的颜色
int nLines = 3;
System.Drawing.Pen pen = new Pen(Color.FromArgb(nRed - 17, nGreen - 17, nBlue - 17), 2);
for (int a = 0; a < nLines; a++)
{
int x1 = rd.Next() % nBmpWidth;
int y1 = rd.Next() % nBmpHeight;
int x2 = rd.Next() % nBmpWidth;
int y2 = rd.Next() % nBmpHeight;
graph.DrawLine(pen, x1, y1, x2, y2);
}
//采用的字符集,可以随即拓展,并可以控制字符出现的几率
string strCode = "23456789ASDFGHJKZXCVBNMQWERTYUIP";
//4、循环取得字符,并绘制
string strResult = "";
for (int i = 0; i < nLen; i++)
{
int x = (i * 13 + rd.Next(3));
int y = rd.Next(4) + 1;
//确定字体
System.Drawing.Font font = new Font("Courier New", 12 + rd.Next() % 4, FontStyle.Bold);
//随机获取字符
char c = strCode[rd.Next(strCode.Length)];
strResult += c.ToString();
//绘制字符
graph.DrawString(c.ToString(), font, new SolidBrush(System.Drawing.Color.FromArgb(nRed - 60 + y * 3, nGreen - 60 + y * 3, nBlue - 40 + y * 3)), x, y);
}
//5、输出字节流
System.IO.MemoryStream bstream = new MemoryStream();
bmp.Save(bstream, System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Dispose();
graph.Dispose();
strKey = strResult;
byte[] byteReturn = bstream.ToArray();
bstream.Close();
return byteReturn;
}
另建一页面test.aspx
<div>
<table>
<tr>
<td height="35">验证码:</td>
<td><asp:TextBox runat="server" ID="txtyz" BorderColor="#333333" BorderWidth="1px" BorderStyle="Solid" Width="180px"></asp:TextBox></td>
<td><asp:Image ID="Image1" runat="server" ImageUrl="Vildate.aspx"/></td>
</tr>
</table>
</div>
<table>
<tr>
<td height="35">验证码:</td>
<td><asp:TextBox runat="server" ID="txtyz" BorderColor="#333333" BorderWidth="1px" BorderStyle="Solid" Width="180px"></asp:TextBox></td>
<td><asp:Image ID="Image1" runat="server" ImageUrl="Vildate.aspx"/></td>
</tr>
</table>
</div>