ASP.NET---- 인증 코드 클래스

24170 단어 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
}
}

좋은 웹페이지 즐겨찾기