string code;
private void CreateImage()
{
code = GetRandomText();
Bitmap bitmap = new Bitmap(200, 50, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Yellow);
Rectangle rect = new Rectangle(0, 0, 200, 50);
SolidBrush b = new SolidBrush(Color.Black);
SolidBrush blue = new SolidBrush(Color.Blue);
int counter = 0;
g.DrawRectangle(pen, rect);
g.FillRectangle(b, rect);
for (int i = 0; i < code.Length; i++)
{
g.DrawString(code[i].ToString(), new Font("Verdena", 10 + rand.Next(14, 18)), blue, new PointF(10 + counter, 10));
counter += 20;
int red = 0;
int white = 11;
while (white <= 100)
{
g.FillRectangle(Brushes.White, 0, red, 200, 10);
g.FillRectangle(Brushes.White, 0, white, 200, 10);
red += 20;
white += 20;
}
}
DrawRandomLines(g);
pictureBox1.Image = bitmap;
}
private void DrawRandomLines(Graphics g)
{
SolidBrush green = new SolidBrush(Color.Green);
for (int i = 0; i < 20; i++)
{
g.DrawLines(new Pen(green, 2), GetRandomPoints());
}
}
private Point[] GetRandomPoints()
{
Point[] points = { new Point(rand.Next(10, 150), rand.Next(10, 150)), new Point(rand.Next(10, 100), rand.Next(10, 100)) };
return points;
}
private string GetRandomText()
{
StringBuilder randomText = new StringBuilder();
if (String.IsNullOrEmpty(code))
{
string alphabets = "abcdefghijklmnopqrstuvwxyz1234567890";
Random r = new Random();
for (int j = 0; j <= 5; j++)
{
randomText.Append(alphabets[r.Next(alphabets.Length)]);
}
code = randomText.ToString();
}
return code;
}