0

这是我的代码:

HTML:<img src="thumbCreate.ashx?Id=223" alt="asd" />

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”,因为它包含错误。” 任何想法?

4

2 回答 2

3

问题来自您的这部分代码。

string resourceId = context.Request.QueryString["Id"];
context.Response.Write("No resource found for Id = " + resourceId);

您总是在响应流中添加一个字符串,然后再写入图像数据,这将导致字符串损坏。删除它(或使其有条件,以便在发生错误或其他情况时添加它),它应该可以工作。

于 2012-10-05T05:34:31.883 回答
0

context.Response.WriteFile() 是否有效?

于 2012-10-05T05:14:38.373 回答