100分 求一个asp.net 图片验证
兄弟们我要的是能用的哦不是从网上扒下来的 ,,扒下来的也行,不过得能用才行哦 嘎嘎
对了比较好看点的图片验证 嘎嘎
100分全部送上
------解决方案--------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
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;
namespace ValidCode
{
/// <summary>
/// 随机产生验证码,并将验证码绘制到图片上。
/// </summary>
public partial class CreateImage : System.Web.UI.Page
{
/// <summary>
/// 生成验证码。
/// </summary>
/// <returns> </returns>
private string GenerateCheckCode() {
int number;
char code;
string checkCode = String.Empty;
// 声明并实例化随机数对象。
Random random = new Random();
for(int i=0; i <5; i++) {
number = random.Next(1000);
if(number < 500)
code = (char)(48 + (number % 10));
else
code = (char)(65 + (number % 26));
checkCode += code.ToString();
}
// 将验证码保存到Session中。
Session[ "CheckCode "] = checkCode;
return checkCode;
}
/// <summary>
/// 利用生成的验证码绘图。
/// </summary>
/// <param name= "checkCode "> 验证码 </param>
private void CreateCheckCodeImage(string checkCode) {
if(checkCode == null || checkCode.Trim() == String.Empty)
return;
// 声明并实例化一个画板对象。
Bitmap image = new Bitmap( (int)Math.Ceiling(checkCode.Length * 12.5), 22);
Graphics g = Graphics.FromImage(image);
try {
//生成随机生成器
Random random = new Random();
// 设置图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for(int i=0; i <50; 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.AliceBlue), x1, y1, x2, y2);
}
Font font = new Font( "Arial ", 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.Black, 1.2F);
// 将验证码绘制到画板上。
g.DrawString(checkCode, font, brush, 1, 1);
//画图片的前景噪音点
for(int i=0; i <300; 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.Png);
// 将输出缓冲区清空。
Response.ClearContent();
Response.Expires = 0;
// 不设置缓存.
Response.CacheControl = "no-cache ";
Response.ContentType = "image/Gif ";
Response.BinaryWrite( ms.ToArray() );
ms.Close();
}
finally {
// 清空对象。
g.Dispose();
image.Dispose();
}
}
protected void Page_Load(object sender, System.EventArgs e)