通过输出字节流的形式生成的图形验证码在IE11里面不显示,其他浏览器正常。单独打开也不显示
IE11:
Firefox/Chrome/360:
生成图片验证码的程序:
public void CreateCodeImage(string codeString, int imgWidth, int imgHeight)
{
string code = codeString.Trim();
HttpContext.Current.Session["Code"] = code;
if (string.IsNullOrEmpty(code))
{
return;
}
if (imgWidth == 0) //宽度
{
imgWidth = (int)Math.Ceiling(Convert.ToDouble(code.Length * 13)); //如果没有设置图片宽度 根据验证码位数设置图片宽度
}
if (imgHeight == 0)
{
imgHeight = 24;
}
Bitmap image = new Bitmap(imgWidth, imgHeight); //创建位图对象
Graphics g = Graphics.FromImage(image);
try
{
//生成随机数种子
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线 10条
for (int i = 0; i < 2; i++)
{
//噪音线起点坐标(x1,y1),终点坐标(x2,y2)
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
//用银色画出噪音线
&
最佳解决方案
图片保存的格式和输出的字节流的ContentType需要保持一致,不然IE无法识别文件类型。
//创建内存流用于输出图片
using (MemoryStream ms = new MemoryStream())
{
//图片格式指定为png
image.Save(ms, ImageFormat.Png);
//清除缓冲区流中的所有输出
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Png"; //输出的字节流的ContentType需要和上面保存的文件格式保持一致,这里也修改为image/Png了
//输出图片的二进制流
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}