asp.Net簡易生成登録コード(数字+大文字と小文字)

3848 ワード

どこかわからないことがあれば、メッセージを残してランダムコードクラスを生成してください:SigowayRandom.cs 
 
  
using System;
namespace RongYi.Model.Common
{
///
/// SigowayRandom
///

public class SigowayRandom
{
#region

///
///
///

///
public static string[] GetCheckCode()
{
string[] strCheckCode = new string[4];

//
int nSeed = Convert.ToInt16(DateTime.Now.Millisecond);
Random random = new Random(nSeed);

// 0-9
strCheckCode[0] = Convert.ToString(random.Next(1, 10));
// a-z、A-Z
strCheckCode[1] = SigowayRandom.GetLetter(random);
strCheckCode[2] = Convert.ToString(random.Next(1, 10));
strCheckCode[3] = SigowayRandom.GetLetter(random);

//
return strCheckCode;
}

#endregion

#region ,

///
/// ,
///

///
private static string GetLetter(Random random)
{
//
int nChar = random.Next(65, 122);

// ASCII
if (nChar >= 91 && nChar <= 96)
{
nChar -= 6;
}

return Convert.ToString((char)nChar);
}

#endregion
}
}

描画検査コードクラス:SigowayDraw.cs
 
  
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;

namespace RongYi.Model.Common
{
///
/// SigowayDraw
///

public class SigowayDraw
{
#region

///
///
///

public SigowayDraw() { }

#endregion

#region

///
///
///

///
public string DrawString()
{
//
Font drawFont = new Font("Arial", 10);
//
Bitmap objBitmap = new Bitmap(50, 20);
//
Graphics objGraphics = Graphics.FromImage(objBitmap);
//
objGraphics.Clear(Color.White);
//
string[] strDrawString = SigowayRandom.GetCheckCode();

//
objGraphics.DrawString(strDrawString[0], drawFont, new SolidBrush(Color.Purple), 1, 2);
objGraphics.DrawString(strDrawString[1], drawFont, new SolidBrush(Color.Green), 12, 2);
objGraphics.DrawString(strDrawString[2], drawFont, new SolidBrush(Color.Red), 24, 2);
objGraphics.DrawString(strDrawString[3], drawFont, new SolidBrush(Color.SteelBlue), 35, 2);

//
objGraphics.DrawLine(Pens.Silver, 5, 10, 40, 3);
objGraphics.DrawLine(Pens.Gray, 10, 5, 45, 15);
objGraphics.DrawLine(Pens.HotPink, 15, 20, 30, 10);
objGraphics.DrawLine(Pens.LightPink, 10, 15, 35, 20);

//
objGraphics.DrawImage(objBitmap, 0, 0);

//
string strFile = HttpRuntime.AppDomainAppPath.ToString() + "/Resource/img/CheckCode.gif";

//
objBitmap.Save(strFile, ImageFormat.Gif);

//
string strCheckCode = string.Empty;
foreach (string strTemp in strDrawString)
{
strCheckCode += strTemp;
}

//
return strCheckCode;
}

#endregion
}
}