ASP.NET----検証コード類

24164 ワード

直接カプセル化された検証コードクラス、乱数生成、ノイズ増加、ピクチャ取得は、このクラスの方法を直接参照して実現できます.コードは以下の通りです.
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
}
}