ASP.NET----验证码类
直接封装好的验证码类,随机数生成、噪点增加、图片获取,直接引用该类方法就可以实现,代码如下:
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Text;
using System;
namespace Core.Common.Tool
{
/// <summary>
/// 生成验证码和验证图片的方法类
/// </summary>
public static class ValidateCode
{
#region 静态方法
/// <summary>
/// 生成验证码
/// </summary>
/// <returns>验证码</returns>
public static string CreateValidateCode()
{
int number;
char code;
string verifyCode = string.Empty;
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));
verifyCode += code.ToString();
}
return verifyCode;
}
/// <summary>
/// 生成验证码图片
/// </summary>
/// <param name="validateCode">验证码</param>
/// <returns>验证码图片</returns>
public static Bitmap CreateValidateCodeImage(string validateCode)
{
if (validateCode == null || validateCode.Trim() == String.Empty)
return null;
Bitmap image = new Bitmap((int)Math.Ceiling((validateCode.Length * 12.5)), 22);
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.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold | 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(validateCode, 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);
return image;
}
catch
{
return null;
}
finally
{
g.Dispose();
image.Dispose();
}
}
/// <summary>
/// 生成验证码图片
/// 并保存到文件(GIF格式)
/// </summary>
/// <param name="validateCode">验证码</param>
/// <param name="validateCodeFilePath">文件路径(包含文件名)</param>
/// <returns>是否生成成功</returns>
public static bool CreateValidateCodeImage(string validateCode, string validateCodeFilePath)
{
if (validateCode == null || validateCode.Trim() == String.Empty)
return false;
Bitmap image = new Bitmap((int)Math.Ceiling((validateCode.Length * 12.5)), 22);
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.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold | 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(validateCode, 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);
image.Save(validateCodeFilePath, System.Drawing.Imaging.ImageFormat.Gif);
return true;
}
catch
{
return false;
}
finally
{
g.Dispose();
image.Dispose();
}
}
#endregion
}
}
广积粮,筑高墙,缓称王