我正在使用一个简单的处理程序在 MVC 视图上显示图像,它在我的本地主机上运行良好,但是当我将它发布到我们的托管服务器时,我收到了这个错误(使用 Fiddler):
GET ImageHandler.ashx?img=b...w=255&level=128&slice=0 500 Internal Server Error
这是视图脚本:
<img id="Image1" alt="image" src="ImageHandler.ashx?img=bmptest.bmp&window=255&level=128&slice=0" height="512" width="512" />
这是处理程序的脚本:
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string sImageFileName = "";
int slice = 0;
int window = 0;
int level = 0;
sImageFileName = context.Request.QueryString["img"].ToLower().Trim();
.....
System.Drawing.Image objImage = null;
Image bmp = Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("content/" + sImageFileName));
MemoryStream objMemoryStream = new MemoryStream();
bmp.Save(objMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageContent = new byte[objMemoryStream.Length];
objMemoryStream.Position = 0;
objMemoryStream.Read(imageContent, 0, (int)objMemoryStream.Length);
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(imageContent);
}
public bool IsReusable
{
get
{
return false;
}
}
}
我通过添加以下内容修改了 web.cong:
<system.web>
......
<httpHandlers>
<add verb="*" path="*.ashx" type="DirectshowTest.ImageHandler,DirectshowTest"/>
</httpHandlers>
</system.web>
将不胜感激您的建议。