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

本文介绍了一个使用ASP.NET处理图像的例子,并详细解释了如何解决在不同浏览器(如IE8、Firefox和Chrome)上显示图像时遇到的问题。通过调整MIME类型设置,确保了图像能在各种现代浏览器中正确显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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",改动后各浏览器测试良好

 

 

 

 


<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值