//创建一个120*50的位图对象
Bitmap bmp = new Bitmap(120, 50);
//创建一个画图对象
Graphics g = Graphics.FromImage(bmp);
Random r = new Random();//创建随机数对象
//随机生成验证码
string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnokqrstuvwxyz0123456789";
string str = "";
for (int i = 0; i < 4; i++)
{
str += s[r.Next(0, s.Length)].ToString();
}
//颜色数组 随机
Color[] colors = { Color.Blue, Color.Red, Color.Black };
//字体数组
string[] fonts = { "微软雅黑", "宋体", "楷体", "黑体" };
//画验证码到bmp
for (int i = 0; i < 4; i++)
{
Point p = new Point(i * 30, 0);
// 要画的字符串 字体 字体大小 格式 颜色
g.DrawString(str[i].ToString(), new Font(fonts[r.Next(0, fonts.Length)], 25, FontStyle.Bold), new SolidBrush(colors[r.Next(0, colors.Length)]), p);
}
//画线
for (int i = 0; i < 30; 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.DeepPink), p1, p2);//设置画笔颜色,两个点的坐标开始画线
}
//画像素
for (int i = 0; i < 200; i++)
{
bmp.SetPixel(r.Next(0, bmp.Width), r.Next(0, bmp.Height), Color.Black);
}
//把画的位图镶嵌到picbox上
pictureBox1.Image = bmp;
|
|