C# 尝试通过ashx处理程序显示图像时出错-图像“;http://localhost:57157/......” 无法显示,因为它包含错误
这是我的密码: HTML:C# 尝试通过ashx处理程序显示图像时出错-图像“;http://localhost:57157/......” 无法显示,因为它包含错误,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,这是我的密码: HTML: HTTP处理程序:` public void ProcessRequest (HttpContext context) { CreateThumbNail(context); } private void CreateThumbNail(HttpContext context) { string resourceId = context.Request.QueryString["Id"]; context
HTTP处理程序:`
public void ProcessRequest (HttpContext context)
{
CreateThumbNail(context);
}
private void CreateThumbNail(HttpContext context)
{
string resourceId = context.Request.QueryString["Id"];
context.Response.Write("No resource found for Id = " + resourceId);
Bitmap original = new Bitmap("C:/Devp/My work/ASHXSampleApp/Images/Desert.jpg");
int oWidth = original.Width;
int oHeight = original.Height;
int preferredWidth = 80;
int preferredHeight = 100;
int thumbWidthFactor = oWidth / preferredWidth;
int thumbHeightFactor = oHeight / preferredHeight;
int maxFactor = Math.Max(thumbWidthFactor, thumbHeightFactor);
int thumbNailWidth = oWidth / maxFactor;
int thumbNailHeight = oHeight / maxFactor;
Bitmap thumbNailImage = (Bitmap)original.GetThumbnailImage(thumbNailWidth, thumbNailHeight, ThumbNailCallback, IntPtr.Zero);
context.Response.ContentType = "image/Jpeg";
thumbNailImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}`
但此代码不显示图像。当我在firefox中手动运行处理程序时,它会给我一个错误:-“图像”http://localhost:57157/ASHXSampleApp/thumbCreate.ashx?Id=223“无法显示,因为它包含错误。”有什么想法吗?context.Response.WriteFile()有效吗?context.Response.WriteFile()有效吗工作?问题来自代码的这一部分
string resourceId = context.Request.QueryString["Id"];
context.Response.Write("No resource found for Id = " + resourceId);
您总是向响应流添加字符串,然后再写入图像数据,这将导致字符串损坏。删除它(或使其有条件,以便在发生错误或其他情况时添加它),它应该可以工作。问题来自代码的这一部分
string resourceId = context.Request.QueryString["Id"];
context.Response.Write("No resource found for Id = " + resourceId);
您总是向响应流添加字符串,然后再写入图像数据,这将导致字符串损坏。删除它(或使其有条件,以便在发生错误或其他情况时添加它),它应该可以工作。请调试您的代码,并使用您找到的异常/消息/值的信息更新问题。调试处理程序代码时没有异常。但是我看不到html页面中呈现的图像。当我尝试在firefox中运行页面时,我可以在控制台中看到错误-“图像损坏或被截断:”。当我手动尝试运行处理程序时,会出现错误-“图像”无法显示,因为它包含错误。我没有在web.config中进行任何设置。我是否需要在web.Config中进行任何设置?您每次都在响应中写入“找不到Id=”的资源,而这在您之后开始写入图像时是行不通的。@Karl Johan Sjögren-Ohh yes。。那是个问题。。现在开始工作了。为什么不把你的评论作为答案呢?请调试你的代码,并用你发现的异常/消息/值更新问题。调试处理程序代码时没有异常。但是我看不到html页面中呈现的图像。当我尝试在firefox中运行页面时,我可以在控制台中看到错误-“图像损坏或被截断:”。当我手动尝试运行处理程序时,会出现错误-“图像”无法显示,因为它包含错误。我没有在web.config中进行任何设置。我是否需要在web.Config中进行任何设置?您每次都在响应中写入“找不到Id=”的资源,而这在您之后开始写入图像时是行不通的。@Karl Johan Sjögren-Ohh yes。。那是个问题。。现在开始工作了。你为什么不把你的评论作为答案?@V4Vendetta:stack的评论权限??无论如何,尝试类似于-context.Response.WriteFile(“C:/Devp/My work/ASHXSampleApp/Images/Desert.jpg”);。这也不管用。@V4Vendetta:stack的评论权限??无论如何,尝试类似于-context.Response.WriteFile(“C:/Devp/My work/ASHXSampleApp/Images/Desert.jpg”);。那也不管用。