首页 > 代码库 > winfrom 底层类 验证码
winfrom 底层类 验证码
效果图:
底层类:
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="len">验证码长度</param>
/// <returns></returns>
private static string CreateRandomCode(int len) {
System.Random rand = new Random();
string randomCode = "";//随机码
char[] Allchar = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘P‘, ‘T‘, ‘Q‘, ‘W‘, ‘X‘, ‘Y‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘j‘, ‘k‘, ‘m‘, ‘n‘, ‘p‘, ‘q‘, ‘t‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘ };
for (int i = 0; i < len; i++) {
//生成一个小于Allchar.Length的随机数
int a = rand.Next(Allchar.Length);
randomCode += Allchar[a].ToString();
}
//验证码
return randomCode;
}
/// <summary>
/// 生成验证码,填充到PictureBox中
/// </summary>
/// <param name="len">验证码长度</param>
/// <param name="pic">PictureBox名称</param>
/// <param name="revolve">旋转度数</param>
/// <param name="picHeight">图片高度</param>
/// <param name="picWidth">图片宽度</param>
/// <param name="charDistance">验证码中各个字符间距</param>
/// <param name="fontSize">验证码字符的大小</param>
public static string CreatePic(int len, PictureBox pic, int revolve, int picHeight, int picWidth, int charDistance, float fontSizes) {
//获取验证码
string strCode = CreateRandomCode(len);
//创建背景图片
Bitmap map = new Bitmap(picWidth, picHeight);
Graphics grap = Graphics.FromImage(map);
//清除画面,填充背景色为AliceBlue
grap.Clear(Color.AliceBlue);
//画一个矩形边框
grap.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - 1, map.Height - 1);
//模式
grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//画噪点
System.Random rand = new Random();
for (int i = 0; i < 50; i++) {
int x = rand.Next(0, map.Width - 1);
int y = rand.Next(0, map.Height - 1);
grap.DrawRectangle(new Pen(Color.Gray, 0), x, y, 1, 1);
}
//把字符串拆成单个字符
char[] chars = strCode.ToCharArray();
//文字居中
StringFormat format = new StringFormat(StringFormatFlags.NoClip);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
//定义颜色
Color[] colors = { Color.Red, Color.Black, Color.Blue, Color.Green, Color.Orange, Color.DarkCyan, Color.Purple };
//定义字体
string[] fonts = { "Arial", "Verdana", "Georgia", "Cambria Math", "华文中宋" };
for (int j = 0; j < len; j++) {
//颜色,字体的随机数,随机数作为下标
int cindex = rand.Next(colors.Length);
int findex = rand.Next(fonts.Length);
//字符颜色
Brush b = new System.Drawing.SolidBrush(colors[cindex]);
//字体样式、大小
System.Drawing.Font f = new System.Drawing.Font(fonts[findex], fontSizes, FontStyle.Bold);
//定义一个点
System.Drawing.Point p = new System.Drawing.Point(16, 20);
//转动的角度数
float angle = rand.Next(-revolve, revolve);
//移动光标到指定位置
grap.TranslateTransform(p.X, p.Y);
//转动angle度
grap.RotateTransform(angle);
//赋值
grap.DrawString(chars[j].ToString(), f, b, 1, 1, format);
//转动回去
grap.RotateTransform(-angle);
//移动光标到指定位置
grap.TranslateTransform(charDistance, -p.Y);
}
pic.Image = map;
pic.SizeMode = PictureBoxSizeMode.StretchImage;
return strCode;
}
前台调用:
说明:
(1)窗体要有一个PictureBox控件
(2)DataAccess 是底层类名称
DataAccess.CreatePic(4, pic, 50, 40, 100, 3, 20);
winfrom 底层类 验证码