C# 如果找不到指定的图像文件,显示默认图像的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/717734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:21:33  来源:igfitidea点击:

Best way to display default image if specified image file is not found?

c#asp.netlanguage-agnostic

提问by roman m

I've got your average e-Commerce app, I store ITEM_IMAGE_NAME in the database, and sometimes managers MISSPELL the image name.

我有一般的电子商务应用程序,我将 ITEM_IMAGE_NAME 存储在数据库中,有时管理人员会忽略图像名称。

To avoid "missing images" (red X in IE), every time I display the list of products, I check the server for the image related to the product, and if that file doesn't exist - I replace it with default image.

为了避免“丢失图像”(IE 中的红色 X),每次我显示产品列表时,我都会检查服务器是否有与产品相关的图像,如果该文件不存在 - 我将其替换为默认图像。

As far as i can tell this doesn't affect performance, but I was wondering if there are any alternatives to fix a "missing image" problem.

据我所知,这不会影响性能,但我想知道是否有任何替代方法可以解决“丢失图像”问题。

I'm using ASP.NET + C# (.NET 3.5)

我正在使用 ASP.NET + C# (.NET 3.5)

Some code:

一些代码:

foreach (Item item in Items)
{
  string path = Path.Combine("~/images/", item.categoryImage);
  item.categoryImage = File.Exists(Server.MapPath(path)) ? item.categoryImage : GetImageDependingOnItemType();
}

采纳答案by Erik

You might consider something with javascript

您可能会考虑使用 javascript

<img src="image.jpg" onerror="this.onerror=null;this.src='default.jpg'">

Edit: Or program a 404.aspx returning a default image, if a nonexisting image was requested.

编辑:如果请求不存在的图像,或者编写一个 404.aspx 返回默认图像。

回答by Thomas L Holaday

You can specify on the server what image should be returned for all requests to non-existent image files. That way the user can get a "2 AWESUM 2 SHO" lolcat instead of a red x.

您可以在服务器上指定对于不存在的图像文件的所有请求应返回什么图像。这样用户可以获得“2 AWESUM 2 SHO”笑脸而不是红色x。

回答by dr. evil

I think your way is pretty much OK. I'd do it in a function or do in .categoryImage accessor.

我觉得你的方法还可以。我会在一个函数中做它或者在 .categoryImage 访问器中做。

回答by tvanfosson

I think I would find a way to make the data consistent rather than allowing users to enter inconsistent data. Perhaps your management app could allow the manager to select an existing image or upload a new one, then set the name of the image based on this input so that you'd be assured that the image will exist. Only remove an image when all references to it have been removed from the database. Restrict the interaction with the data to your app so that people can't make those sorts of mistakes.

我想我会找到一种方法来使数据保持一致,而不是让用户输入不一致的数据。也许您的管理应用程序可以允许经理选择现有图像或上传新图像,然后根据此输入设置图像名称,以便您确信该图像将存在。仅当对图像的所有引用都已从数据库中删除时才删除图像。将与数据的交互限制在您的应用程序中,这样人们就不会犯这些错误。

Another way to handle this would be to have a handler (or a controller in ASP.NET MVC) that does the image lookup based on id and returns the image data. Coupled with caching this could be very efficient and would allow you to do image replacement as well.

处理此问题的另一种方法是使用处理程序(或 ASP.NET MVC 中的控制器)根据 id 执行图像查找并返回图像数据。结合缓存,这可能非常有效,并且还允许您进行图像替换。

回答by ephemient

<style>
  .fallback { background-image: url("fallback.png"); }
</style>

<img class="fallback" src="missing.png" width="400" height="300">

If missing.pngloads, it will cover the space allocated for it, as if the fallback were not specified. (Assuming it's not transparent.)

如果missing.png加载,它将覆盖为其分配的空间,就好像没有指定回退一样。(假设它不透明。)

If missing.pngfails to load, the space will instead be filled with fallback.png. You'll still get a little "broken image" icon, but I prefer it that way... a little hint that says "fix me".

如果missing.png加载失败,空间将被填充fallback.png。你仍然会得到一个“破碎的图像”图标,但我更喜欢那样......一个小提示,说“修复我”。

If your images aren't all the same size, you'll notice that the background tiles by default. You can use background-repeat: no-repeat;if you don't like that.

如果您的图像大小不同,您会注意到默认情况下背景是平铺的。background-repeat: no-repeat;不喜欢的可以用。

回答by Andrew

I like Erik's solution, but without removing the event after the first execution, because if you are using that code in, let's say, an MVC partial view, this will work only the first time it is loaded. So I'd go with:

我喜欢 Erik 的解决方案,但在第一次执行后没有删除事件,因为如果您在 MVC 部分视图中使用该代码,则这只会在第一次加载时起作用。所以我会选择:

<img src="image.jpg" onerror="this.src='default.jpg';" />

In case you have many images in the same situation, like a grid, you can do this instead:

如果您在相同情况下有许多图像,例如网格,您可以这样做:

$("img").on("error", function () {
    $(this).attr('src', 'default.jpg');
});

Of course, you may want to use a more specific jQuery selector.

当然,您可能希望使用更具体的 jQuery 选择器。