5

我在我的智慧在这里结束。

我有一个 HTTP 处理程序 (ImageHandler.ashx) 发送图像(调整大小),它是一个标准 HTTP 处理程序(使用可重用的 true 和 false 进行尝试),它使用 Image.GetThumbnailImage 来调整大小并返回缩略图。

我有一个带有 html 图像控件的表的 asp Datalist 控件。

     <asp:DataList ID="listImg" runat="server" RepeatColumns="4" RepeatDirection="Horizontal"
        ShowFooter="false" ShowHeader="false">
        <ItemTemplate>
            <table width="220px">
                <tr width="100%">
                    <td>
                        <img src="Scripts/ImageHandler.ashx?width=125&image=Upload/<%# DataBinder.Eval(Container.DataItem, "photo") %>"                              
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:DataList>

如您所见,处理程序需要的参数是宽度和图像路径。

此 Datalist 绑定到提供要显示的图像列表的数据表 (ImageData)。

到目前为止一切都说得通,现在是问题所在 - 假设我正在加载 5 张图像,即我的 ImageData DataTable 有 5 行,假设只显示 3-4 张图像,剩下的只是想出一个红色的 X,就像你没有图像时一样。现在,如果您查看代码并导航到图像 src,例如 -

    http://localhost:3540/Scripts/ImageHandler.ashx?width=150&image=Upload/Test123.jpg

你会看到图像,它们都没有丢失的图像。重新加载,他们回来了。

我在 Firefox 中运行它并打开了 Firebug,当我查看图像选项卡时,所有图像都根据 Firebug 返回(状态 200 OK,我在响应选项卡中看到图像),就像 Web 服务器不显示一些其中。请注意,处理/加载时间最长的图像并不总是丢失,而是一些随机图像。

这里会发生什么?

谢谢你。

编辑 1 - 添加处理程序代码(原始),我们继承了此代码。我已经从这里的代码中删除了缓存,但仅供参考的缩略图一旦生成就会被缓存。

    public class ImageHandler : IHttpHandler{
public int _width;
public int _height;
public int _percent;
public string imageURL;


public void ProcessRequest(HttpContext context)
{
    try
    {
        Bitmap bitOutput;

        string appPath = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["ImagePath"]);

        String strArquivo = appPath + context.Request.QueryString["image"].Replace("/", "\\");

        if (!(String.IsNullOrEmpty(context.Request["width"])))
        {
            Bitmap bitInput = GetImage(context);

           if (SetHeightWidth(context, bitInput))
            { bitOutput = ResizeImage(bitInput, _width, _height, _percent); }
            else { bitOutput = bitInput; }

            context.Response.ContentType = "image/jpeg";
            bitOutput.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        return;

    }
    catch (Exception ex) { /*HttpContext.Current.Response.Write(ex.Message);*/ }
}


/// <summary>
/// Get the image requested via the query string. 
/// </summary>
public Bitmap GetImage(HttpContext context)
{
    try
    {
        if (context.Cache[("ImagePath-" + context.Request.QueryString["image"])] == null)
        {
            string appPath = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["ImagePath"]);

            appPath = appPath + context.Request.QueryString["image"].Replace("/", "\\");
            Bitmap bitOutput;
            imageURL = appPath;


            bitOutput = new Bitmap(appPath);
            return bitOutput;
        }
        else
        {
            return (Bitmap)context.Cache[("ImagePath-" + context.Request.QueryString["image"])];
        }

    }
    catch (Exception ex) { throw ex; }
}    


/// <summary>
/// Set the height and width of the handler class.
/// </summary>
public bool SetHeightWidth(HttpContext context, Bitmap bitInput)
{
    try
    {
        double inputRatio = Convert.ToDouble(bitInput.Width) / Convert.ToDouble(bitInput.Height);

        if (!(String.IsNullOrEmpty(context.Request["width"])) && !(String.IsNullOrEmpty(context.Request["height"])))
        {
            _width = Int32.Parse(context.Request["width"]);
            _height = Int32.Parse(context.Request["height"]);
            return true;
        }
        else if (!(String.IsNullOrEmpty(context.Request["width"])))
        {
            _width = Int32.Parse(context.Request["width"]);
            _height = Convert.ToInt32((_width / inputRatio));
            if (_width == 400 &&_height > 500)
            {
                _height = 500;
                _width = Convert.ToInt32(500 * inputRatio);
            }
            else if (_width == 125 && _height > 200)
            {
                _height = 200;
                _width = Convert.ToInt32(200 * inputRatio);
            }
            return true;
        }
        else if (!(String.IsNullOrEmpty(context.Request["height"])))
        {
            _height = Int32.Parse(context.Request["height"]);
            _width = Convert.ToInt32((_height * inputRatio));
            return true;
        }
        else if (!(String.IsNullOrEmpty(context.Request["percent"])))
        {
            _height = bitInput.Height;
            _width = bitInput.Width;
            _percent = Int32.Parse(context.Request["percent"]);
            return true;
        }
        else
        {
            _height = bitInput.Height;
            _width = bitInput.Width;
            return false;
        }

    }
    catch (Exception ex) { throw ex; }
}

/// <summary>
/// Resizes bitmap using high quality algorithms.
/// </summary>
public static Bitmap ResizeImage(Bitmap originalBitmap, int newWidth, int newHeight, int newPercent)
{
    try
    {
        if (newPercent != 0)
        {
            newWidth = Convert.ToInt32(originalBitmap.Width * (newPercent * .01));
            newHeight = Convert.ToInt32(originalBitmap.Height * (newPercent * .01));
        }

        Bitmap inputBitmap = originalBitmap;
        Bitmap resizedBitmap = new Bitmap(newWidth, newHeight, PixelFormat.Format64bppPArgb);

        Graphics g = Graphics.FromImage(resizedBitmap);
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        Rectangle rectangle = new Rectangle(0, 0, newWidth, newHeight);
        g.DrawImage(inputBitmap, rectangle, 0, 0, inputBitmap.Width, inputBitmap.Height, GraphicsUnit.Pixel);
        g.Dispose();

        return resizedBitmap;

    }
    catch (Exception ex) { throw ex; }
}


public bool IsReusable
{
    get
    {
        return true;
    }
}}

编辑 2如果有人希望测试整个事情,这里是从预定义文件夹中随机选取图像以创建 aspDataList (listImg) 绑定到的 DataTable (ImageData) 的代码 -

        System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(Server.MapPath("Upload"));
        System.IO.FileInfo[] files = dirInfo.GetFiles();

        int fileCount = files.Length;

        System.Data.DataTable ImageData = new System.Data.DataTable();
        System.Data.DataColumn dCol = new System.Data.DataColumn("photo");
        ImageData.Columns.Add(dCol);


        System.Random rnd = new Random();
        int nxtNumber = 0;

        System.Data.DataRow dRow = null;

        string fileName = string.Empty;

        for (int i = 0; i < 20; i++)
        {
            dRow = ImageData.NewRow();
            nxtNumber = rnd.Next(fileCount);
            while (!files[nxtNumber].Extension.Equals(".jpg"))
            {
                nxtNumber = rnd.Next(fileCount);
            }
            fileName = files[nxtNumber].Name;

            dRow["photo"] = fileName;

            ImageData.Rows.Add(dRow);
        }
        listImg.DataSource = ImageData;
        listImg.DataBind();
4

3 回答 3

0

好吧,有趣的是——服务器上的相同代码以更可预测的方式运行,我可能仍然有一些丢失的图像,但现在它就像平均从 30-40 中丢失 1 个,而在每 5 个中丢失 1 个我当地的环境。由于这些请求是异步的,它是否与运行它的机器的实际 CPU 有关,与我那可怜的笔记本电脑相比,服务器更适合处理多个请求?

无论哪种方式我都修改了代码,所以此时不​​涉及调整大小,处理程序代码只是获取调整大小的图像,此时一切都很好。

谢谢大家的意见。

于 2013-10-23T14:19:33.293 回答
0

我认为您的实际代码在 img 标签末尾包含 /> ???如果没有,您可以尝试先添加它并重新测试。

不是真正的答案,但您可以尝试将 img 标签更改为 asp:Label 并将文件名显示为文本,而不是显示图像只是为了查看数据表是否正确构建。正如您在直接访问图像处理程序时能够看到图像一样,我会说它与代码隐藏中的数据表构造有关。

当我做过类似的事情时,我通常将图像存储在 SQL DB 中,并使用图像处理程序在查询字符串中返回带有记录 ID 的图像。这意味着您将返回一个现有的记录表,而不必根据文件夹的内容创建一个。

于 2013-09-22T14:30:37.903 回答
0

尝试编码 url 的查询字符串,例如:

http://localhost:3540/Scripts/ImageHandler.ashx?width=150&image=Upload/Test123.jpg

网址编码为

http://localhost:3540/Scripts/ImageHandler.ashx?width%3D150%26image%3DUpload%2FTest123.jpg
于 2013-05-14T16:17:11.580 回答