接着上一篇G++,介绍一下用它写成的验证码,代码如下:
private void picString_Click(object sender, EventArgs e)
{
Random r = new Random();
string strNumber = "";
for (int i = 0; i < 5; i++)
{
int num = r.Next(10);
//把随机数存到字符串中
strNumber += num;
}
//位图来存放图片
//设置位图大小
Bitmap bm = new Bitmap(252, 213);
//创建对象
Graphics g = Graphics.FromImage(bm);
//画验证码
//五种字体
string[] fontName = new string[] { "幼圆", "宋体", "隶书", "仿宋", "楷体" };
//颜色
Color[]color=new Color[]{Color.Red ,Color.Black ,Color.White,Color .Blue ,Color .Pink };
for (int i = 0; i < 5; i++)
{
Point point = new Point(i * 120, 0);
//随机的字体、颜色;大小40磅
g.DrawString(strNumber, new Font(fontName [i],
40), new SolidBrush (color [i]), point);
}
for (int i = 0; i < 103; i++)
{
Point p_1 = new Point(r.Next(bm.Width)/2, r.Next(bm.Height)/2);
Point p_2 = new Point(r.Next(bm.Width), r.Next(bm.Height)/2);
//画线(线条颜色为灰色,起点为 (p_1, p_2)
g.DrawLine(new Pen(Brushes.Gray), p_1, p_2);
}
for (int i = 0; i < 1510; i++)
{
//设置点,颜色为yellow
Point p = new Point(r.Next(bm.Width), r.Next(bm.Height));
bm.SetPixel(p.X, p.Y, Color.Yellow);
}
//画验证码
picString.Image = bm;
}
写的不是很perfect,二八定律,有时间再研究。
THANKS FOR YOUR TIME