C# 如何从 CSS 引用嵌入的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1196747/
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
How to reference embedded images from CSS?
提问by Max Schmeling
I have a CSS file that is embedded in my assembly. I need to set a background image for certain elements using this CSS file, and the image needs to be an embedded resource also. Is this possible? Is there any way I can reliably do this?
我有一个嵌入在我的程序集中的 CSS 文件。我需要使用此 CSS 文件为某些元素设置背景图像,并且图像也需要是嵌入资源。这可能吗?有什么办法可以可靠地做到这一点吗?
I ran into the problem when putting an existing stylesheet into this dll then realized images weren't showing up. I don't know of any way to make it work though because I would need to know the URL to the embedded image.
我在将现有的样式表放入这个 dll 时遇到了这个问题,然后意识到图像没有显示出来。我不知道有什么方法可以使它工作,因为我需要知道嵌入图像的 URL。
Has anyone done anything like this?
有没有人做过这样的事情?
采纳答案by Akash Kava
<% = WebResource("image1.jpg") %>
You can use above statement inside your CSS file, and while you register your CSS with WebResourceAttribute, you can set "PerformSubstitution" to true
您可以在 CSS 文件中使用上述语句,并且在使用 WebResourceAttribute 注册 CSS 时,可以将“PerformSubstitution”设置为 true
Default.css
body{
background: <%=WebResource("xyz.jpg")%>
}
[assembly, WebResource("Default.css","text/css", PerformSubstitution=true)]
[assembly, WebResource("xyz.jpg","image/jpg")]
回答by maxwellb
What about exposing the resources through a Web service?
Such as in the CSS file, set background: url( getImage.aspx?image=newyork.jpg )
?
如何通过 Web 服务公开资源?比如在 CSS 文件中,设置background: url( getImage.aspx?image=newyork.jpg )
?
回答by IsolatedStorage
Mine is a slight variation on the other suggestions but it works for my inline CSS within my ASP.NET page
我的与其他建议略有不同,但它适用于我的 ASP.NET 页面中的内联 CSS
- Add the following entry to the AssemblyInfo.cs file -
[assembly: WebResource("MyImageFile.png", "image/png")]
- add the following code within the CSS to reference the embedded resource -
background-image: url('<%= Page.ClientScript.GetWebResourceUrl(typeof(MyUserControl), "MyImageFile.png") %>')
- 将以下条目添加到 AssemblyInfo.cs 文件中 -
[assembly: WebResource("MyImageFile.png", "image/png")]
- 在 CSS 中添加以下代码以引用嵌入的资源 -
background-image: url('<%= Page.ClientScript.GetWebResourceUrl(typeof(MyUserControl), "MyImageFile.png") %>')
回答by Reishabh
Just follow the following steps to refer a web resource as background Image in CSS
只需按照以下步骤在 CSS 中将 Web 资源引用为背景图像
Refer Image URL as "background: url('<%=WebResource("xyz.jpg")%>');" in following manner.
Default.css body{ background: url('<%=WebResource("xyz.jpg")%>'); }
In AssemblyInfo.cs file register the CSS file with "PerformSubstitution=true" attribute in following manner
[assembly, WebResource("Default.css","text/css", PerformSubstitution=true)]
Now again in AssemblyInfo.cs file register the image file as
[assembly, WebResource("xyz.jpg","image/jpg")]
Right Click the Image File (xyz.jpg) and CSS File (Default.css) and click on Properties now select "Build Resource" option as "Embedded Resource".
将图像 URL 引用为“背景:url('<%=WebResource("xyz.jpg")%>');” 以下列方式。
Default.css body{ background: url('<%=WebResource("xyz.jpg")%>'); }
在 AssemblyInfo.cs 文件中,按以下方式使用“PerformSubstitution=true”属性注册 CSS 文件
[assembly, WebResource("Default.css","text/css", PerformSubstitution=true)]
现在再次在 AssemblyInfo.cs 文件中将图像文件注册为
[assembly, WebResource("xyz.jpg","image/jpg")]
右键单击图像文件 (xyz.jpg) 和 CSS 文件 (Default.css),然后单击属性,现在选择“构建资源”选项作为“嵌入式资源”。
and its done. Happy Coding !!!
它完成了。快乐编码!!!