///
/// 生成验证码
///
///
///
private void pictureBox1_Click(object sender, EventArgs e)
{
//产生随机数数组
Random r = new Random();
string str = null;
for (int i = 0; i < 5; i++)
{
int rnumber = r.Next(0, 10);
str += rnumber;
}
//创建图像与GDI对象
Bitmap bmp = new Bitmap(150,40);
Graphics g = Graphics.FromImage(bmp);
//将字符随机字体及颜色画到图片上
for (int i = 0; i < 5; i++)
{
Point p = new Point(i * 20, 0);
string[] font = { "微软雅黑", "宋体", "黑体", "隶书", "仿宋" };
Color[] colors = { Color.Red, Color.Blue, Color.Green, Color.DarkOliveGreen, Color.Cyan };
g.DrawString(str[i].ToString(),new Font(font[r.Next (0,5)],15,FontStyle.Bold),new SolidBrush(colors[r.Next(0,5)]),p);
}
//随机线条
for (int i = 0; i < 25; i++)
{
Point p1 = new Point (r.Next(0,bmp.Width),r.Next(0,bmp.Height));
Point p2 = new Point (r.Next(0,bmp.Width),r.Next(0,bmp.Height));
g.DrawLine(new Pen(Brushes.Green), p1, p2);
}
//随机像素点
for (int i = 0; i < 500; i++)
{
Point p = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
bmp.SetPixel(p.X, p.Y, Color.Black);
}
//将图片嵌入到PcBox中
pictureBox1.Image = bmp;
}