Reputation: 582
In Razor I have a loop that gives me results. In that loop, I have a Url.Action that serves an image:
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>
}
The image served is dependent on if the image for that particular row exists. If it doesn't, an alternate image is served.
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));
}
}
Now this is fine, and works pretty fast if there are a few results. If there are thousands of results, there is some definite slowdown in serving all those instances of images. Is there a more efficient way of doing this?
I could be serving less results in the model but that's not what I'm aiming for in this particular question.
Upvotes: 3
Views: 2036
Reputation: 17182
Some options I would say -
Upvotes: 2