1.
GSC_WebControlLibrary 这是在网上找到的一个控件,非常好用。但是效果不是特别好(见下图。
)虽然容易使用,所有的属性都可以像控件一样设置,但是可用性不太高。用户不能自定义,而且看起来这个验证码效果不太好。
效果:
![](https://images.cnblogs.com/cnblogs_com/ucetgg/107396/t_1.bmp)
2.用一个页面生成图片,另一个页面调用,验证码存入cookie,调用时取cookie对比验证.这个用户就可以按自己的喜好更改效果和验证码的长度了 (:
效果如图:
![](https://images.cnblogs.com/cnblogs_com/ucetgg/107396/t_2.bmp)
代码如下:
CheckCode.aspx
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
![](/Images/OutliningIndicators/None.gif)
public partial class Tools_CheckCode : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.CreateCheckCodeImage(GenerateCheckCode());
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
private string GenerateCheckCode()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int number;
char code;
string checkCode = String.Empty;
![](/Images/OutliningIndicators/InBlock.gif)
System.Random random = new Random();
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 5; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
number = random.Next();
![](/Images/OutliningIndicators/InBlock.gif)
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
![](/Images/OutliningIndicators/InBlock.gif)
checkCode += code.ToString();
}
![](/Images/OutliningIndicators/InBlock.gif)
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
![](/Images/OutliningIndicators/InBlock.gif)
return checkCode;
}
![](/Images/OutliningIndicators/InBlock.gif)
private void CreateCheckCodeImage(string checkCode)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
![](/Images/OutliningIndicators/InBlock.gif)
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//生成随机生成器
Random random = new Random();
![](/Images/OutliningIndicators/InBlock.gif)
//清空图片背景色
g.Clear(Color.White);
![](/Images/OutliningIndicators/InBlock.gif)
//画图片的背景噪音线
for (int i = 0; i < 25; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
![](/Images/OutliningIndicators/InBlock.gif)
g.DrawLine(new Pen(Color.GreenYellow), x1, y1, x2, y2);
}
![](/Images/OutliningIndicators/InBlock.gif)
Font font = new System.Drawing.Font("Verdana", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
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);
![](/Images/OutliningIndicators/InBlock.gif)
//画图片的前景噪音点
for (int i = 0; i < 80; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
![](/Images/OutliningIndicators/InBlock.gif)
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
![](/Images/OutliningIndicators/InBlock.gif)
//画图片的边框线
g.DrawRectangle(new Pen(Color.Red), 0, 0, image.Width - 1, image.Height - 1);
![](/Images/OutliningIndicators/InBlock.gif)
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
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
g.Dispose();
image.Dispose();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
然后在需要使用的页面引用:
UseCheckCode.aspx
<img src="Tools/CheckCode.aspx" alt="验证码" style="width: 60px; height: 24px" />3.用web handler生成图片。这个其实和前面的意思大致差不多,调用方法也基本和2一样,不同的是,他的验证码是存入Session的。供学习参考。
效果图如下:
ValidateImageHandler.ashx
%@ WebHandler Language="C#" Class="ValidateImageHandler" %>
![](/Images/OutliningIndicators/None.gif)
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// ValidateImageHandler 生成网站验证码功能
/// </summary>
public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int intLength = 5; //长度
string strIdentify = "Identify"; //随机字串存储键值,以便存储到Session中
public ValidateImageHandler()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 生成验证图片核心代码
/// </summary>
/// <param name="hc"></param>
public void ProcessRequest(HttpContext hc)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//设置输出流图片格式
hc.Response.ContentType = "image/gif";
Bitmap b = new Bitmap(200, 60);
Graphics g = Graphics.FromImage(b);
g.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 200, 60);
Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
![](/Images/OutliningIndicators/InBlock.gif)
//合法随机显示字符列表
string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder s = new StringBuilder();
//将随机生成的字符串绘制到图片上
for (int i = 0; i < intLength; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
s.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(Color.Blue), i * 38, r.Next(0, 15));
}
![](/Images/OutliningIndicators/InBlock.gif)
//生成干扰线条
Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
for (int i = 0; i < 10; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
g.DrawLine(pen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
}
b.Save(hc.Response.OutputStream, ImageFormat.Gif);
hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用户输入是否一致
hc.Response.End();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
/// </summary>
public bool IsReusable
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
}