context.Response.ContentType = "image/jpg"导致的无法显示 XML 页

使用asp.net处理图像:

<%@ WebHandler Language="C#" Class="MakeImage" %>

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;

public class MakeImage : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/jpeg";
string imgPath = "Upload\\1.jpg";
using (Image bmp = Image.FromFile(context.Server.MapPath(imgPath)))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawString("steve",new Font("微软雅黑",16),Brushes.Black,bmp.Width - 60,bmp.Height - 30);
bmp.Save(context.Response.OutputStream,ImageFormat.Jpeg);
}
}
}

public bool IsReusable {
get {
return false;
}
}

}

使用IE8出现如下错误

无法显示 XML 页。

使用 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。

文本内容中发现无效字符。处理资源 'http://localhost:7151/WebOne/MakeImage.ashx' 时出错。

而用firefox和chrome测试却并未报错,纠结了老半天才发现原来是设置了context.Response.ContentType = "image/jpg"的原因,遂将context.Response.ContentType = "image/jpg"语句删除,IE8打开页面显示成功!但是在firefox、chrome里打开却出现了乱码。

随后在网上找原因,原来是”image/jpep“而不是“image/jpg",改动后各浏览器测试良好

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念