gpt4 book ai didi

c# - 如何在 ASP.NET MVC 中有效地提供图像

转载 作者:行者123 更新时间:2023-11-30 17:01:08 24 4
gpt4 key购买 nike

在 Razor 中,我有一个给出结果的循环。在那个循环中,我有一个提供图像的 Url.Action:

foreach (var i in Model.InventoryList)
{
<tr>
<td style="text-align: center;"><img alt="@i.ProductCode" src="@Url.Action("GetImageThumbnail", "ServiceRequest", new { ProductCode = i.ProductCode })" onclick="showGallery('@i.ProductCode');" /></td>
</tr>
}

提供的图像取决于该特定行的图像是否存在。如果没有,则提供替代图像。

public ActionResult GetImageThumbnail(string productCode)
{
string filePath = "path_to_product.png";
Size size = new Size(80, 50);
if (!System.IO.File.Exists(filePath))
{
filePath = "No_image_available.png";
size = new Size(50, 50);
}
using (MemoryStream ms = new MemoryStream()){
Image image = Image.FromFile(filePath);
image = ImageHelpers.ResizeImage(image, size);
image.Save(ms, ImageHelpers.getImageFormat(filePath));
return File(ms.ToArray(), ImageHelpers.getContentType(filePath));
}

}

现在这很好,如果有一些结果,工作起来会非常快。如果有数以千计的结果,那么在提供所有这些图像实例时肯定会变慢。有没有更有效的方法来做到这一点?

我可以在模型中提供较少的结果,但这不是我在这个特定问题中的目标。

最佳答案

我会说的一些选项 -

  • 你可以DiskCache Plugin IIS 用于调整大小的图像。与其即时进行所有调整大小的计算,不如在创建图像时保存调整大小的图像。以后如果您不想调整图像大小,可以随时将其删除。
  • 其他一些插件,如 SourceMemCache、OutputMemCache 和 SourceDiskCache,也可用于 IIS 的 DiskCache 插件本身。
  • 或者,您可以将所有调整大小的图像写入缓存提供程序,如 Redis.io或到 Squid
  • Lazy Load images on your page
  • 尝试重新设计您的页面,我的意思是即使它是图库页面,也请尝试使用分页,这样图库中的数千张图片将归结为单个页面上的几十张。

关于c# - 如何在 ASP.NET MVC 中有效地提供图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21373051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com