以下验证码的代码,倒数第4行,<this.Response.ContentType = "image/jpeg";>这句存在的意义是啥,我理解不了?我换成"text/html","text/plain","application/x-excel"......都看不出啥影响,把这段直接删掉也没影响,F5调试都是跳出验证码,求大神解释。public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(80, 40);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.DrawRectangle(Pens.Black, 0, 0, 79, 39);
Color[] colors = { Color.Black, Color.Red, Color.Blue, Color.DarkGreen, Color.Purple, Color.DarkGoldenrod, Color.Chocolate };
string[] fontNames = { "宋体", "楷体_GB2321", "隶书", "Arial", "Comic Sans MS", "Microsoft Sans Serif", "Times New Roman" };
Random rand = new Random();
char[] chs = new char[] { (char)(65 + rand.Next(26)), (char)(65 + rand.Next(26)), (char)(65 + rand.Next(26)) };
Brush brush;
Font font;
int x, y;
for (int i = 0; i < 3; i++)
{
brush = new SolidBrush(colors[rand.Next(7)]);
font = new Font(fontNames[rand.Next(7)], 18, FontStyle.Bold);
x = i * 20 + 2;
y = 5 + rand.Next(5);
g.RotateTransform(rand.Next(-10, 9));
g.DrawString(chs[i].ToString(), font, brush, x, y);
} Pen[] pens = { Pens.Gray, Pens.LightGray };
for (int i = 0; i < 200; i++)
{
x = rand.Next(bmp.Width - 1);
y = rand.Next(bmp.Height - 1);
g.DrawEllipse(pens[i % 2], x, y, 1, 1);
}
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Jpeg);
this.Response.Clear();
this.Response.ContentType = "image/jpeg"; //这段代码的作用?我用F5调试的时候看不出来,有它没它好像都没区别啊?
this.Response.BinaryWrite(ms.ToArray());
g.Dispose();
bmp.Dispose();
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(80, 40);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.DrawRectangle(Pens.Black, 0, 0, 79, 39);
Color[] colors = { Color.Black, Color.Red, Color.Blue, Color.DarkGreen, Color.Purple, Color.DarkGoldenrod, Color.Chocolate };
string[] fontNames = { "宋体", "楷体_GB2321", "隶书", "Arial", "Comic Sans MS", "Microsoft Sans Serif", "Times New Roman" };
Random rand = new Random();
char[] chs = new char[] { (char)(65 + rand.Next(26)), (char)(65 + rand.Next(26)), (char)(65 + rand.Next(26)) };
Brush brush;
Font font;
int x, y;
for (int i = 0; i < 3; i++)
{
brush = new SolidBrush(colors[rand.Next(7)]);
font = new Font(fontNames[rand.Next(7)], 18, FontStyle.Bold);
x = i * 20 + 2;
y = 5 + rand.Next(5);
g.RotateTransform(rand.Next(-10, 9));
g.DrawString(chs[i].ToString(), font, brush, x, y);
} Pen[] pens = { Pens.Gray, Pens.LightGray };
for (int i = 0; i < 200; i++)
{
x = rand.Next(bmp.Width - 1);
y = rand.Next(bmp.Height - 1);
g.DrawEllipse(pens[i % 2], x, y, 1, 1);
}
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Jpeg);
this.Response.Clear();
this.Response.ContentType = "image/jpeg"; //这段代码的作用?我用F5调试的时候看不出来,有它没它好像都没区别啊?
this.Response.BinaryWrite(ms.ToArray());
g.Dispose();
bmp.Dispose();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货