C#验证码
/// <summary>
///验证码
/// </summary>
public class VerifyCode
{
/// <summary>
/// 验证码字体大小 ( 为了显示扭曲效果,默认 40 像素,可以自行修改 )
/// </summary>
int FontSize = 40;
/// <summary>
/// 文字间距
/// </summary>
int Padding = 3;
/// <summary>
/// 自定义背景色 ( 默认白色 )
/// </summary>
Color BackgroundColor = Color.White;
/// <summary>
/// 自定义随机颜色数组
/// </summary>
Color[] Colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
/// <summary>
/// 自定义字体数组
/// </summary>
string[] Fonts = { "Arial", "Georgia", "courier new" };
/// <summary>
/// 自定义随机码字符串序列 ( 使用逗号分隔 )
/// </summary>
string CodeSerial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
#region 产生波形滤镜效果
private const double PI2 = 6.283185307179586476925286766559;
/// <summary>
/// 正弦曲线 Wave 扭曲图片( Edit By 51aspx.com )
/// </summary>
/// <param name="srcBmp"> 图片路径 </param>
/// <param name="bXDir"> 如果扭曲则选择为 True </param>
/// <param name="dMultValue"> 波形的幅度倍数,越大扭曲的程度越高,一般为 3 </param>
/// <param name="dPhase"> 波形的起始相位,取值区间 [0-2*PI) </param>
/// <returns></returns>
public byte[] TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
using (Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height))
{
// 将位图背景填充为白色
using (Graphics graph = Graphics.FromImage(destBmp))
{
graph.FillRectangle(new SolidBrush(Color.White), 0, 0, destBmp.Width, destBmp.Height);
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 = (bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen) + dPhase;
double dy = Math.Sin(dx);
// 取得当前点的颜色
int nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
int nOldY = bXDir ? j : j + (int)(dy * dMultValue);
Color color = srcBmp.GetPixel(i, j);
if (nOldX >= 0 && nOldX < destBmp.Width && nOldY >= 0 && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX, nOldY, color);
}
}
}
}
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
destBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.GetBuffer();
}
}
}
#endregion
#region 生成校验码图片
public byte[] CreateImageCode(int codeLen = 6)
{
string code = CreateVerifyCode(codeLen);
int fSize = FontSize;
int fWidth = fSize + Padding;
int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2;
int imageHeight = fSize * 2 + Padding;
using (Bitmap image = new Bitmap(imageWidth, imageHeight))
{
using (Graphics g = Graphics.FromImage(image))
{
g.Clear(BackgroundColor);
Random rand = new Random();
int left = 0, top = 0;
int n1 = (imageHeight - FontSize - Padding * 2);
int n2 = n1 / 4;
Font f;
Brush b;
int cindex, findex;
// 随机字体和颜色的验证码字符
for (int i = 0; i < code.Length; i++)
{
cindex = rand.Next(Colors.Length - 1);
findex = rand.Next(Fonts.Length - 1);
f = new Font(Fonts[findex], fSize, FontStyle.Bold);
b = new SolidBrush(Colors[cindex]);
if (i % 2 == 1)
{
top = n2 * 2;
}
else
{
top = n2;
}
left = i * fWidth;
g.DrawString(code.Substring(i, 1), f, b, left, top);
}
}
// 产生波形
return TwistImage(image, true, 8, 4);
}
}
#endregion
#region 生成随机字符码
public string CreateVerifyCode(int codeLen)
{
string[] arr = CodeSerial.Split(',');
string code = "";
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < codeLen; i++)
{
code += arr[rand.Next(0, arr.Length - 1)];
}
return code;
}
#endregion
}


浙公网安备 33010602011771号