protected void Page_Load(object sender, EventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("Image/a.jpg"));
string txt = getVialdata();
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
g.DrawString(txt, new Font("宋体", 15), new SolidBrush(Color.Black), 5, 5);
Response.ContentType = "Image/Jpeg";
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
private string getVialdata()
{
string text = "";
Random rand = new Random();
for (int i = 0; i < 5; i++)
{
if (rand.Next(100) % 2 == 0)
{
text += (char)rand.Next('A', 'Z');
}
else
{
text += rand.Next(10).ToString();
}
}
Session["id"] = text;
return text;
}
{
System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("Image/a.jpg"));
string txt = getVialdata();
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
g.DrawString(txt, new Font("宋体", 15), new SolidBrush(Color.Black), 5, 5);
Response.ContentType = "Image/Jpeg";
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
private string getVialdata()
{
string text = "";
Random rand = new Random();
for (int i = 0; i < 5; i++)
{
if (rand.Next(100) % 2 == 0)
{
text += (char)rand.Next('A', 'Z');
}
else
{
text += rand.Next(10).ToString();
}
}
Session["id"] = text;
return text;
}
通过Session["id"]去判断是否输入正确