用户注册等都需要用到验证码
public ValidCode(int len, CodeType ctype)
{
this._len = len;
this._codetype = ctype;
}
#endregion
#region Public Field
public enum CodeType { Words, Numbers, Characters, Alphas }
#endregion
#region Private Methods
private string GenerateNumbers()
{
string strOut = "";
System.Random random = new Random();
for (int i = 0; i < _len; i )
{
string num = Convert.ToString(random.Next(10000) % 10);
strOut = num;
}
return strOut.Trim();
}
private string GenerateCharacters()
{
string strOut = "";
System.Random random = new Random();
for (int i = 0; i < _len; i )
{
string num = Convert.ToString((char)(65 random.Next(10000) % 26));
strOut = num;
}
return strOut.Trim();
}
//
private string GenerateAlphas()
{
string strOut = "";
string num = "";
System.Random random = new Random();
for (int i = 0; i < _len; i )
{
if (random.Next(500) % 2 == 0)
{
num = Convert.ToString(random.Next(10000) % 10);
}
else
{
num = Convert.ToString((char)(65 random.Next(10000) % 26));
}
strOut = num;
}
return strOut.Trim();
}
private System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
// 将位图背景填充为白色
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
graph.Dispose();
double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
for (int i = 0; i < destBmp.Width; i )
{
for (int j = 0; j < destBmp.Height; j )
{
double dx = 0;
dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
dx = dPhase;
double dy = Math.Sin(dx);
// 取得当前点的颜色
int nOldX = 0, nOldY = 0;
nOldX = bXDir ? i (int)(dy * dMultValue) : i;
nOldY = bXDir ? j : j (int)(dy * dMultValue);
System.Drawing.Color color = srcBmp.GetPixel(i, j);
if (nOldX >= 0 && nOldX < destBmp.Width
&& nOldY >= 0 && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX, nOldY, color);
}
}
}
return destBmp;
}
评论