生成校验文字图片,如网页上的登录校验码(类:SubmitCoder)
生成校验文字图片,如网页上的登录校验码(类:SubmitCoder)
生成校验文字图片,如网页上的登录校验码。SubmitCoder类生成Win程序界面的校验码。
public enum SubmitCodeMode
{
Charater = 0,
Number = 1,
MixedMode = 2
}
public class SubmitCoder
{
private Font _CodeFont;
private Size _CodeImageSize = new Size();
private SubmitCodeMode _Mode = SubmitCodeMode.MixedMode;
public SubmitCoder(Size codeImageSize, SubmitCodeMode mode, Font codeFont)
{
_CodeFont = codeFont;
_CodeImageSize = codeImageSize;
_Mode = mode;
}
/// <summary>
/// build sumbitcode character
/// </summary>
public string GenerateCheckCode()
{
int CODE_LENGTH = 5;
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
int count = 1;
while (true)
{
number = random.Next();
if (_Mode == SubmitCodeMode.MixedMode)
{
if (number % 2 == 0)
code = (char)(’0’ + (char)(number % 10));
else
code = (char)(’A’ + (char)(number % 26));
}
else if (_Mode == SubmitCodeMode.Number)
code = (char)(’0’ + (char)(number % 10));
else if (_Mode == SubmitCodeMode.Charater)
code = (char)(’A’ + (char)(number % 26));
else continue;
checkCode += code.ToString();
count += 1;
if (count > CODE_LENGTH) break;
}
return checkCode;
}
/// <summary>
/// build picture
/// </summary>
/// <param name="checkCode"></param>
public Image BuildSumbitCodeImage(string checkCode)
{
//System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 36);
System.Drawing.Bitmap image = new Bitmap(_CodeImageSize.Width, _CodeImageSize.Height);
Graphics g = Graphics.FromImage(image);
try
{
//build random number
Random random = new Random();
//clean picture backcolor
g.Clear(Color.White);
//draw picture background yamp line
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 System.Drawing.Font("Arial", _CodeFont.Size, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
//Font font = _CodeFont;
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(checkCode, font, brush, 2, 2);
//draw picutre foregound yamp dot
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
}
//draw picture boderline
g.DrawRectangle(new Pen(Color.Green), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
System.IO.Stream sr = ms;
return System.Drawing.Image.FromStream(sr, true, true);
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
{
Charater = 0,
Number = 1,
MixedMode = 2
}
public class SubmitCoder
{
private Font _CodeFont;
private Size _CodeImageSize = new Size();
private SubmitCodeMode _Mode = SubmitCodeMode.MixedMode;
public SubmitCoder(Size codeImageSize, SubmitCodeMode mode, Font codeFont)
{
_CodeFont = codeFont;
_CodeImageSize = codeImageSize;
_Mode = mode;
}
/// <summary>
/// build sumbitcode character
/// </summary>
public string GenerateCheckCode()
{
int CODE_LENGTH = 5;
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
int count = 1;
while (true)
{
number = random.Next();
if (_Mode == SubmitCodeMode.MixedMode)
{
if (number % 2 == 0)
code = (char)(’0’ + (char)(number % 10));
else
code = (char)(’A’ + (char)(number % 26));
}
else if (_Mode == SubmitCodeMode.Number)
code = (char)(’0’ + (char)(number % 10));
else if (_Mode == SubmitCodeMode.Charater)
code = (char)(’A’ + (char)(number % 26));
else continue;
checkCode += code.ToString();
count += 1;
if (count > CODE_LENGTH) break;
}
return checkCode;
}
/// <summary>
/// build picture
/// </summary>
/// <param name="checkCode"></param>
public Image BuildSumbitCodeImage(string checkCode)
{
//System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 36);
System.Drawing.Bitmap image = new Bitmap(_CodeImageSize.Width, _CodeImageSize.Height);
Graphics g = Graphics.FromImage(image);
try
{
//build random number
Random random = new Random();
//clean picture backcolor
g.Clear(Color.White);
//draw picture background yamp line
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 System.Drawing.Font("Arial", _CodeFont.Size, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
//Font font = _CodeFont;
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(checkCode, font, brush, 2, 2);
//draw picutre foregound yamp dot
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
}
//draw picture boderline
g.DrawRectangle(new Pen(Color.Green), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
System.IO.Stream sr = ms;
return System.Drawing.Image.FromStream(sr, true, true);
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网