Random random = new Random();
Bitmap image = new Bitmap(190, 47);//(190,47)为pictureBox1的大小
Graphics g = Graphics.FromImage(image);
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 5; i++)
{
Point p1 = new Point();
p1.X = random.Next(image.Width);
p1.Y = random.Next(image.Height);
Point p2 = new Point();
p2.X = random.Next(image.Width);
p2.Y = random.Next(image.Height);
g.DrawLine(new Pen(Color.Blue), p1, p2);
}
Font font = new Font("Arial", 33, System.Drawing.FontStyle.Bold);
g.DrawString(CheckCode(), font, new SolidBrush(Color.Red), 1, 5);
//画图片的前景噪音点
for (int i = 0; i < 2000; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
this.pictureBox1.BackgroundImage = image;