1

我将数据库中存储图像的数据作为 byte[] 数组获取;然后我将其转换为 System.Drawing.Image 如下所示的代码;

  public System.Drawing.Image CreateImage(byte[] bytes)
        {

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes);
            System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);
            return image;
        }

(*) 另一方面,当客户端向下滚动页面时,我计划在 asp.net 页面上显示图像列表。用户在他/她确实看到更多照片的页面上下来和下来的次数越多。所以这意味着快速的页面加载和丰富的用户体验。(您可能会在 www.mashable.com 上看到我的意思,向下滚动时请注意新加载的照片。)

此外,从上述方法返回的 imgae 对象,我如何使用上面的 (*) 条件在循环中动态显示它。

问候 bk

4

1 回答 1

1

好吧,我认为主要瓶颈实际上是每次您需要图像时都会访问数据库。(特别是考虑到许多用户访问该网站。)

我会采用以下解决方案:

  1. 数据库将存储原始质量的图像;
  2. .ashx 处理程序将以各种所需的分辨率在文件系统上缓存图像(例如图标为 32x32 像素,缩略图为 48x48 等),根据请求返回它们并仅访问数据库一次;(在示例中显示了如何通过 ashx 处理程序返回图像)
  3. 实际页面将指向 .ashx 页面以获取图像。(喜欢<img scr="GetImage.ashx?ID=324453&Size=48" />

更新:

因此处理程序中的实际工作流程将如下所示:

    public void ProcessRequest (HttpContext context)
    {
        // Create path of cached file based on the context passed
        int size = Int32.Parse(context.Request["Size"]);
        // For ID Guids are possibly better
        // but it can be anything, even parameter you need to pass
        // to the web service in order to get those bytes
        int id = Int32.Parse(context.Request["Id"]);
        string imagePath = String.Format(@"images/cache/{0}/{1}.png", size, id);

        // Check whether cache image exists and created less than an hour ago
        // (create it if necessary)
        if (!File.Exists(imagePath)
            || File.GetLastWriteTime(imagePath) < DateTime.Now.AddHours(-1))
        {
            // Get the file from the web service here
            byte[] imageBytes = ...

            // Save as a file
            using (var memoryStream = new MemoryStream(imageBytes))
            using (var outputStream = File.OpenWrite(imagePath))
                Image.FromStream(memoryStream).Save(outputStream);
        }

        context.Response.ContentType = "image/png";
        context.Response.WriteFile(imagePath);
    }
于 2010-05-07T08:52:26.470 回答