asp.net验证码-拿来主义,直接就能用
来源:互联网 发布:新后缀域名 编辑:程序博客网 时间:2024/05/22 17:10
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System;
using System.Web;
using System.Web.Security;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public partial class validate_img : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateCheckCodeImage(GenerateCheckCode());
}
/// <summary>
/// 生成字符串
/// </summary>
/// <returns></returns>
private string GenerateCheckCode()
{
int number;
char code; //字符码
string checkCode = String.Empty,sendcoder;
System.Random random = new Random();
for (int i = 0; i < 5; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
{
code = (char)('A' + (char)(number % 26));
}
checkCode += code.ToString();
}
sendcoder = checkCode.ToLower();
//把字符串放进cookie里
Response.Cookies.Add(new HttpCookie("CheckCode", sendcoder));
return checkCode;
}
/// <summary>
/// 生成图片
/// </summary>
/// <param name="checkCode"></param>
public void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 13.5)), 25);//高为25
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 25; 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.GreenYellow), x1, y1, x2, y2);
}
//写字符
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);
//画图片的前景噪音点
for (int i = 0; i < 80; 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.Red), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
- asp.net验证码-拿来主义,直接就能用
- 时钟HTML,直接粘贴就能用
- Asp.net验证码
- ASP.net 验证码
- ASP.NET验证码
- Asp.net验证码
- Asp.net验证码
- asp.net验证码
- asp.net 验证码
- ASP.NET 验证码
- asp.net验证码
- ASP.NET 验证码
- asp.net验证码
- asp.net验证码
- Asp.net验证码
- asp.net 验证码
- ASP.NET验证码
- Asp.net验证码
- 网站建设流程
- IE6/IE7/IE8 下关闭页面不提示
- c的智慧
- 【转】使用System.arraycopy()实现数组之间的复制
- oracle 创建表和复制表,复制表结构,复制表数据 查询前N行
- asp.net验证码-拿来主义,直接就能用
- Snow Leopard 终端命令
- 不要在公共接口中传递STL容器
- ServerSocket选项的基本解析:SO_TIMEOUT选项、SO_REUSEADDR选项、SO_RCVBUF选项
- oracle form builder创建的form在本机运行
- 免费OA办公系统评测 到底哪个适合本公司?
- Android2.1源代码结构
- SQL SERVER数据库开发之存储过程应用---[Microsoft Sql Server 2005]
- wince下sourcessources.cmnMakefile.def的相关作用