一种新的验证码(改进版)
Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Security.Cryptography;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
namespace Test.CheckCode
{
/// <summary>
/// 举行左上角坐标和X、Y距离
/// </summary>
public class Coordinate
{
private int x;
private int y;
private int xDistance = 0;
private int yDistance = 0;
/// <summary>
/// X距离
/// </summary>
public int XDistance
{
get { return xDistance; }
set { xDistance = value; }
}
/// <summary>
/// Y距离
/// </summary>
public int YDistance
{
get { return yDistance; }
set { yDistance = value; }
}
/// <summary>
/// X点坐标
/// </summary>
public int X
{
get { return x; }
set { x = value; }
}
/// <summary>
/// Y点坐标
/// </summary>
public int Y
{
get { return y; }
set { y = value; }
}
}
/// <summary>
/// 反色矩形集合生产器
/// </summary>
public class ReverseCoordinates
{
/// <summary>
/// 单件模式 实例
/// </summary>
public static readonly ReverseCoordinates Instance = new ReverseCoordinates();
/// <summary>
/// 总长度
/// </summary>
public const int TotalLenght = 650;
/// <summary>
/// 总宽度
/// </summary>
public const int TotalHeight = 250;
private List<Coordinate> list = new List<Coordinate>();
private ReverseCoordinates()
{ }
/// <summary>
/// 返回要反色矩形左上角坐标的集合
/// </summary>
/// <returns>反色矩形左上角坐标的集合</returns>
public List<Coordinate> GetList()
{
this.CreateList();
return this.list;
}
/// <summary>
/// 创建反色矩形左上角坐标的集合
/// </summary>
private void CreateList()
{
Random random = new Random(Guid.NewGuid().GetHashCode());
//创建4个角反色矩形的长和宽
int cornerXDistance = random.Next(1, Convert.ToInt32(TotalLenght / 15));
int cornerYDistance = random.Next(1, Convert.ToInt32(TotalLenght / 5));
this.list = new List<Coordinate>();
//创建左上角反色矩形
Coordinate coordinate = new Coordinate();
coordinate.X = 1;
coordinate.Y = 1;
coordinate.XDistance = cornerXDistance;
coordinate.YDistance = cornerYDistance;
this.list.Add(coordinate);
//创建左下角反色矩形
coordinate = new Coordinate();
coordinate.X = 1;
coordinate.Y = TotalHeight - cornerYDistance;
coordinate.XDistance = cornerXDistance;
coordinate.YDistance = cornerYDistance;
this.list.Add(coordinate);
//创建右上角反色矩形
coordinate = new Coordinate();
coordinate.X = TotalLenght - cornerXDistance;
coordinate.Y = 1;
coordinate.XDistance = cornerXDistance;
coordinate.YDistance = cornerYDistance;
this.list.Add(coordinate);
//创建右下角反色矩形
coordinate = new Coordinate();
coordinate.X = TotalLenght - cornerXDistance;
coordinate.Y = TotalHeight - cornerYDistance;
coordinate.XDistance = cornerXDistance;
coordinate.YDistance = cornerYDistance;
this.list.Add(coordinate);
//创建中间大的反色矩形
coordinate = new Coordinate();
//得到每个字的大概大小
int fontSize = Convert.ToInt32(TotalLenght / 3.3);
//得到每个字大概中间的部位的X值
int[] sizeArray = new int[4];
sizeArray[0] = fontSize;
sizeArray[1] = fontSize * 1 - fontSize;
sizeArray[2] = fontSize * 2 - fontSize;
sizeArray[3] = fontSize * 3 - fontSize;
//随机选取中间反色矩形的X值
int index = random.Next(0, 3);
//得到中间反色矩形和左上角矩形的距离
int beginX = sizeArray[index] - cornerXDistance;
//如果两个矩形重叠
if (beginX < 0)
{
//将负的距离变正数
beginX *= -1;
}
else
{
beginX = 0;
}
//为了每次中间的反色矩形位置都不一样计算偏移值
int offset = random.Next(beginX, Convert.ToInt32(fontSize / 2));
coordinate.X = sizeArray[index] + offset;
coordinate.Y = random.Next(1, Convert.ToInt32(TotalHeight / 2));
coordinate.XDistance = random.Next(Convert.ToInt32(TotalLenght / 6), Convert.ToInt32(TotalLenght / 3));
coordinate.YDistance = random.Next(Convert.ToInt32(TotalHeight / 2), TotalHeight - 1);
this.list.Add(coordinate);
}
}
/// <summary>
/// 验证码生成器
/// </summary>
public class ValidateCode
{
private string checkCode;
/// <summary>
/// 验证码
/// </summary>
public string CheckCode
{
get { return checkCode; }
set { checkCode = value; }
}
public ValidateCode()
{
this.GenerateCheckCode();
}
/// <summary>
/// 创建随机字符串
/// </summary>
private void GenerateCheckCode()
{
int number;
char code;
this.checkCode = String.Empty;
Random random = new Random();
for (int i = 0; i < 4; i++)
{
number = random.Next();
code = (char)('0' + (char)(number % 10));
this.checkCode += code.ToString();
}
}
/// <summary>
/// 生产图片
/// </summary>
public void CreateCheckCodeImage()
{
ReverseCoordinates coordinates = ReverseCoordinates.Instance;
int totalLenght = ReverseCoordinates.TotalLenght;
int totalHeight = ReverseCoordinates.TotalHeight;
Bitmap image = null;
Graphics graphics = null;
MemoryStream ms = null;
try
{
image = new Bitmap(totalLenght + 1, totalHeight + 1);
graphics = Graphics.FromImage(image);
ms = new MemoryStream();
//清空图片背景色
graphics.Clear(Color.White);
Font fontA = new Font("Arial", Convert.ToInt32(totalLenght / 3.3), (FontStyle.Bold | FontStyle.Italic));
Random random = new Random(Guid.NewGuid().GetHashCode());
int offset = random.Next(-5, 0);
graphics.DrawString(checkCode, fontA, Brushes.Black, offset, offset);
Color pixel;
List<Coordinate> list = coordinates.GetList();
foreach (Coordinate coordinate in list)
{
int x = coordinate.X;
for (int i = 0; i < coordinate.XDistance && x < totalLenght; i++, x++)
{
int y = coordinate.Y;
for (int j = 0; j < coordinate.YDistance && y < totalHeight; j++, y++)
{
int r, g, b;
pixel = image.GetPixel(x, y);
r = 255 - pixel.R;
g = 255 - pixel.G;
b = 255 - pixel.B;
image.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
}
//画图片的边框线
graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
image.Save(ms, ImageFormat.Gif);
HttpResponse httpResponse = HttpContext.Current.Response;
httpResponse.ClearContent();
httpResponse.Expires = 0;
httpResponse.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
httpResponse.ContentType = "image/Gif";
httpResponse.BinaryWrite(ms.ToArray());
}
finally
{
graphics.Dispose();
image.Dispose();
ms.Dispose();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Web;
using System.Security.Cryptography;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
namespace Test.CheckCode
{
/// <summary>
/// 举行左上角坐标和X、Y距离
/// </summary>
public class Coordinate
{
private int x;
private int y;
private int xDistance = 0;
private int yDistance = 0;
/// <summary>
/// X距离
/// </summary>
public int XDistance
{
get { return xDistance; }
set { xDistance = value; }
}
/// <summary>
/// Y距离
/// </summary>
public int YDistance
{
get { return yDistance; }
set { yDistance = value; }
}
/// <summary>
/// X点坐标
/// </summary>
public int X
{
get { return x; }
set { x = value; }
}
/// <summary>
/// Y点坐标
/// </summary>
public int Y
{
get { return y; }
set { y = value; }
}
}
/// <summary>
/// 反色矩形集合生产器
/// </summary>
public class ReverseCoordinates
{
/// <summary>
/// 单件模式 实例
/// </summary>
public static readonly ReverseCoordinates Instance = new ReverseCoordinates();
/// <summary>
/// 总长度
/// </summary>
public const int TotalLenght = 650;
/// <summary>
/// 总宽度
/// </summary>
public const int TotalHeight = 250;
private List<Coordinate> list = new List<Coordinate>();
private ReverseCoordinates()
{ }
/// <summary>
/// 返回要反色矩形左上角坐标的集合
/// </summary>
/// <returns>反色矩形左上角坐标的集合</returns>
public List<Coordinate> GetList()
{
this.CreateList();
return this.list;
}
/// <summary>
/// 创建反色矩形左上角坐标的集合
/// </summary>
private void CreateList()
{
Random random = new Random(Guid.NewGuid().GetHashCode());
//创建4个角反色矩形的长和宽
int cornerXDistance = random.Next(1, Convert.ToInt32(TotalLenght / 15));
int cornerYDistance = random.Next(1, Convert.ToInt32(TotalLenght / 5));
this.list = new List<Coordinate>();
//创建左上角反色矩形
Coordinate coordinate = new Coordinate();
coordinate.X = 1;
coordinate.Y = 1;
coordinate.XDistance = cornerXDistance;
coordinate.YDistance = cornerYDistance;
this.list.Add(coordinate);
//创建左下角反色矩形
coordinate = new Coordinate();
coordinate.X = 1;
coordinate.Y = TotalHeight - cornerYDistance;
coordinate.XDistance = cornerXDistance;
coordinate.YDistance = cornerYDistance;
this.list.Add(coordinate);
//创建右上角反色矩形
coordinate = new Coordinate();
coordinate.X = TotalLenght - cornerXDistance;
coordinate.Y = 1;
coordinate.XDistance = cornerXDistance;
coordinate.YDistance = cornerYDistance;
this.list.Add(coordinate);
//创建右下角反色矩形
coordinate = new Coordinate();
coordinate.X = TotalLenght - cornerXDistance;
coordinate.Y = TotalHeight - cornerYDistance;
coordinate.XDistance = cornerXDistance;
coordinate.YDistance = cornerYDistance;
this.list.Add(coordinate);
//创建中间大的反色矩形
coordinate = new Coordinate();
//得到每个字的大概大小
int fontSize = Convert.ToInt32(TotalLenght / 3.3);
//得到每个字大概中间的部位的X值
int[] sizeArray = new int[4];
sizeArray[0] = fontSize;
sizeArray[1] = fontSize * 1 - fontSize;
sizeArray[2] = fontSize * 2 - fontSize;
sizeArray[3] = fontSize * 3 - fontSize;
//随机选取中间反色矩形的X值
int index = random.Next(0, 3);
//得到中间反色矩形和左上角矩形的距离
int beginX = sizeArray[index] - cornerXDistance;
//如果两个矩形重叠
if (beginX < 0)
{
//将负的距离变正数
beginX *= -1;
}
else
{
beginX = 0;
}
//为了每次中间的反色矩形位置都不一样计算偏移值
int offset = random.Next(beginX, Convert.ToInt32(fontSize / 2));
coordinate.X = sizeArray[index] + offset;
coordinate.Y = random.Next(1, Convert.ToInt32(TotalHeight / 2));
coordinate.XDistance = random.Next(Convert.ToInt32(TotalLenght / 6), Convert.ToInt32(TotalLenght / 3));
coordinate.YDistance = random.Next(Convert.ToInt32(TotalHeight / 2), TotalHeight - 1);
this.list.Add(coordinate);
}
}
/// <summary>
/// 验证码生成器
/// </summary>
public class ValidateCode
{
private string checkCode;
/// <summary>
/// 验证码
/// </summary>
public string CheckCode
{
get { return checkCode; }
set { checkCode = value; }
}
public ValidateCode()
{
this.GenerateCheckCode();
}
/// <summary>
/// 创建随机字符串
/// </summary>
private void GenerateCheckCode()
{
int number;
char code;
this.checkCode = String.Empty;
Random random = new Random();
for (int i = 0; i < 4; i++)
{
number = random.Next();
code = (char)('0' + (char)(number % 10));
this.checkCode += code.ToString();
}
}
/// <summary>
/// 生产图片
/// </summary>
public void CreateCheckCodeImage()
{
ReverseCoordinates coordinates = ReverseCoordinates.Instance;
int totalLenght = ReverseCoordinates.TotalLenght;
int totalHeight = ReverseCoordinates.TotalHeight;
Bitmap image = null;
Graphics graphics = null;
MemoryStream ms = null;
try
{
image = new Bitmap(totalLenght + 1, totalHeight + 1);
graphics = Graphics.FromImage(image);
ms = new MemoryStream();
//清空图片背景色
graphics.Clear(Color.White);
Font fontA = new Font("Arial", Convert.ToInt32(totalLenght / 3.3), (FontStyle.Bold | FontStyle.Italic));
Random random = new Random(Guid.NewGuid().GetHashCode());
int offset = random.Next(-5, 0);
graphics.DrawString(checkCode, fontA, Brushes.Black, offset, offset);
Color pixel;
List<Coordinate> list = coordinates.GetList();
foreach (Coordinate coordinate in list)
{
int x = coordinate.X;
for (int i = 0; i < coordinate.XDistance && x < totalLenght; i++, x++)
{
int y = coordinate.Y;
for (int j = 0; j < coordinate.YDistance && y < totalHeight; j++, y++)
{
int r, g, b;
pixel = image.GetPixel(x, y);
r = 255 - pixel.R;
g = 255 - pixel.G;
b = 255 - pixel.B;
image.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
}
//画图片的边框线
graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
image.Save(ms, ImageFormat.Gif);
HttpResponse httpResponse = HttpContext.Current.Response;
httpResponse.ClearContent();
httpResponse.Expires = 0;
httpResponse.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
httpResponse.ContentType = "image/Gif";
httpResponse.BinaryWrite(ms.ToArray());
}
finally
{
graphics.Dispose();
image.Dispose();
ms.Dispose();
}
}
}
}
www.bluersw.com