加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

context.Response.ContentType = "image/jpg"导致

发布时间:2020-12-15 23:42:36 所属栏目:百科 来源:网络整理
导读:使用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 contex

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读