33

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?

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.

Has anyone done anything like this?

Max Schmeling
  • 12,363
  • 14
  • 66
  • 109

4 Answers4

53
<% = 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

Default.css
body{
    background: <%=WebResource("xyz.jpg")%>
}



[assembly, WebResource("Default.css","text/css", PerformSubstitution=true)]
[assembly, WebResource("xyz.jpg","image/jpg")]
Akash Kava
  • 39,066
  • 20
  • 121
  • 167
  • 10
    I found when using background-image rather than background I needed to wrap the WebResource server tag with url(''). E.g. background-image: url('<%=WebResource("xyz.jpg") %>'); – Daniel Ballinger Sep 30 '10 at 00:00
6

Just follow the following steps to refer a web resource as background Image in CSS

  1. Refer Image URL as "background: url('<%=WebResource("xyz.jpg")%>');" in following manner.

    Default.css
    body{
          background: url('<%=WebResource("xyz.jpg")%>');
        }
    
  2. In AssemblyInfo.cs file register the CSS file with "PerformSubstitution=true" attribute in following manner

    [assembly, WebResource("Default.css","text/css", PerformSubstitution=true)]
    
  3. Now again in AssemblyInfo.cs file register the image file as

    [assembly, WebResource("xyz.jpg","image/jpg")]
    
  4. Right Click the Image File (xyz.jpg) and CSS File (Default.css) and click on Properties now select "Build Resource" option as "Embedded Resource".

and its done. Happy Coding !!!

Reishabh
  • 167
  • 4
  • 8
4

Mine is a slight variation on the other suggestions but it works for my inline CSS within my ASP.NET page

  1. Add the following entry to the AssemblyInfo.cs file - [assembly: WebResource("MyImageFile.png", "image/png")]
  2. add the following code within the CSS to reference the embedded resource - background-image: url('<%= Page.ClientScript.GetWebResourceUrl(typeof(MyUserControl), "MyImageFile.png") %>')
IsolatedStorage
  • 1,175
  • 12
  • 12
0

What about exposing the resources through a Web service? Such as in the CSS file, set background: url( getImage.aspx?image=newyork.jpg )?

maxwellb
  • 13,366
  • 2
  • 25
  • 35
  • This is a good idea except that the resource is embedded in our common library, not in a web app. So unfortunately this won't work for me in this situation. – Max Schmeling Jul 28 '09 at 21:22
  • Then, reference the common library from a new web app, which is solely responsible for pulling out embedded resources from the common library? – maxwellb Jul 28 '09 at 21:34