C# 图像验证码的实现
2011-07-17 09:03
225 查看
原文链接:http://dev.mjxy.cn/a-170.aspx
// Captcha Image
public class CaptchaImage
{
// Public properties (all read-only).
public string Text
{
get { return this.text; }
}
public Bitmap Image
{
get { return this.image; }
}
public int Width
{
get { return this.width; }
}
public int Height
{
get { return this.height; }
}
// Internal properties.
private string text;
private int width;
private int height;
private string familyName;
private Bitmap image;
// For generating random numbers.
private Random random = new Random();
// ====================================================================
// Initializes a new instance of the CaptchaImage class using the
// specified text, width and height.
// ====================================================================
public CaptchaImage(int length, int width, int height) : this(length, width, height, null)
{
}
// ====================================================================
// Initializes a new instance of the CaptchaImage class using the
// specified text, width, height and font family.
// ====================================================================
public CaptchaImage(int length, int width, int height, string familyName)
{
this.text = GenerateRandomText(length);
this.SetDimensions(width, height);
this.SetFamilyName(familyName);
this.GenerateImage();
}
private string GenerateRandomText(int length)
{
Random rnd = new Random();
List<char> table = new List<char>();
for (char i = 'A'; i <= 'Z'; i++)
table.Add(i);
for (char i = '0'; i <= '9'; i++)
table.Add(i);
string retVal = "";
for (int i = 0; i < length; i++)
retVal += table[rnd.Next(table.Count)];
return retVal;
}
// ====================================================================
// This member overrides Object.Finalize.
// ====================================================================
~CaptchaImage()
{
Dispose(false);
}
// ====================================================================
// Releases all resources used by this object.
// ====================================================================
public void Dispose()
{
GC.SuppressFinalize(this);
this.Dispose(true);
}
// ====================================================================
// Custom Dispose method to clean up unmanaged resources.
// ====================================================================
protected virtual void Dispose(bool disposing)
{
if (disposing)
// Dispose of the bitmap.
this.image.Dispose();
}
// ====================================================================
// Sets the image width and height.
// ====================================================================
private void SetDimensions(int width, int height)
{
// Check the width and height.
if (width <= 0)
throw new ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero.");
if (height <= 0)
throw new ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero.");
this.width = width;
this.height = height;
}
// ====================================================================
// Sets the font used for the image text.
// ====================================================================
private void SetFamilyName(string familyName)
{
// If the named font is not installed, default to a system font.
try
{
Font font = new Font(this.familyName, 12F);
this.familyName = familyName;
font.Dispose();
}
catch (Exception ex)
{
this.familyName = System.Drawing.FontFamily.GenericSerif.Name;
}
}
// ====================================================================
// Creates the bitmap image.
// ====================================================================
private void GenerateImage()
{
.......
}
// Captcha Image
public class CaptchaImage
{
// Public properties (all read-only).
public string Text
{
get { return this.text; }
}
public Bitmap Image
{
get { return this.image; }
}
public int Width
{
get { return this.width; }
}
public int Height
{
get { return this.height; }
}
// Internal properties.
private string text;
private int width;
private int height;
private string familyName;
private Bitmap image;
// For generating random numbers.
private Random random = new Random();
// ====================================================================
// Initializes a new instance of the CaptchaImage class using the
// specified text, width and height.
// ====================================================================
public CaptchaImage(int length, int width, int height) : this(length, width, height, null)
{
}
// ====================================================================
// Initializes a new instance of the CaptchaImage class using the
// specified text, width, height and font family.
// ====================================================================
public CaptchaImage(int length, int width, int height, string familyName)
{
this.text = GenerateRandomText(length);
this.SetDimensions(width, height);
this.SetFamilyName(familyName);
this.GenerateImage();
}
private string GenerateRandomText(int length)
{
Random rnd = new Random();
List<char> table = new List<char>();
for (char i = 'A'; i <= 'Z'; i++)
table.Add(i);
for (char i = '0'; i <= '9'; i++)
table.Add(i);
string retVal = "";
for (int i = 0; i < length; i++)
retVal += table[rnd.Next(table.Count)];
return retVal;
}
// ====================================================================
// This member overrides Object.Finalize.
// ====================================================================
~CaptchaImage()
{
Dispose(false);
}
// ====================================================================
// Releases all resources used by this object.
// ====================================================================
public void Dispose()
{
GC.SuppressFinalize(this);
this.Dispose(true);
}
// ====================================================================
// Custom Dispose method to clean up unmanaged resources.
// ====================================================================
protected virtual void Dispose(bool disposing)
{
if (disposing)
// Dispose of the bitmap.
this.image.Dispose();
}
// ====================================================================
// Sets the image width and height.
// ====================================================================
private void SetDimensions(int width, int height)
{
// Check the width and height.
if (width <= 0)
throw new ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero.");
if (height <= 0)
throw new ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero.");
this.width = width;
this.height = height;
}
// ====================================================================
// Sets the font used for the image text.
// ====================================================================
private void SetFamilyName(string familyName)
{
// If the named font is not installed, default to a system font.
try
{
Font font = new Font(this.familyName, 12F);
this.familyName = familyName;
font.Dispose();
}
catch (Exception ex)
{
this.familyName = System.Drawing.FontFamily.GenericSerif.Name;
}
}
// ====================================================================
// Creates the bitmap image.
// ====================================================================
private void GenerateImage()
{
.......
}
相关文章推荐
- 实现验证码图像文字的识别(C#调用DLL)
- 实现验证码图像文字的识别(C#调用DLL)
- 黑白二值图像周长测量--C#实现
- 用C#实现中文验证码
- ASP.net 验证码实现代码(C#)
- Asp.net(C#)实现验证码功能
- Asp.net(C#)实现验证码功能
- C#实现的一款比较美观的验证码完整实例
- C#实现的ASP.NET全能型验证码控件(09.11.22) (10.01.27最新修改)
- C#结合GDAL实现图像乘运算
- C#实现验证码编写
- C#中Bitmap类实现对图像操作的一些方法(Asp.net(C#)-放大缩小图片尺寸)
- C#实现图像反色的方法
- ASP.Net实现验证码(c#)
- 图像处理中几个基本的处理方法c#代码实现
- Asp.net(C#)实现验证码功能
- 转载:请教:c#中实现图像缩放?
- C#实现验证码
- 在ASP.NET下实现数字和字符相混合的验证码(C# Code)
- 【图像处理】C#实现哈哈镜效果