c#如何实现图片随机?请教下各位 最好有详细代码!谢谢
namespace 验证码
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random rnd = new Random();
string pictur;
private void button1_Click(object sender, EventArgs e)
{
//生成随机数
string dd = Convert.ToChar(rnd.Next(65, 90)).ToString();
string ee = Convert.ToChar(rnd.Next(65,90)).ToString();
string ff = Convert.ToChar(rnd.Next(65, 90)).ToString();
string gg = Convert.ToChar(rnd.Next(65, 90)).ToString();
pictur = dd + ee + ff + gg;
grap();
}
private void grap()
{
System.Drawing.Bitmap bmp = new Bitmap(100,50);
Graphics graph = Graphics.FromImage(bmp);
graph.Clear(Color.FromArgb(232, 238, 247));
////画噪点
for (int i = 0; i <= 128; i++)
{
graph.DrawRectangle(
new Pen(Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255))),
rnd.Next(2, 256),
rnd.Next(2, 50),
1,
1);
}
//画出表达式
for (int i = 0; i < pictur.Length; i++)
{
graph.DrawString(pictur.Substring(i, 1),
new Font(FontFamily.GenericSansSerif, 25, FontStyle.Bold),
new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(128), rnd.Next(255))),
5 + i * 20,
rnd.Next(1, 5));
}
//画边框
graph.DrawRectangle(new Pen(Color.Firebrick), 0, 0, 100 - 1, 50 - 1);
pictureBox1.Image = bmp;
}
}
}