12 Mar 2009

Embed images in your web DLLs

You can embed all sorts of stuff into an assembly - javascript, css, pdf etc. Here I quickly detail how to embed an image file.

1. You have a solution with a web project and a separate class project
2. In Visual Studio, drag the image you want to embed into your class project
3. In the Properties window of the Image, select Build Action: Embedded Resource
4. Add a new class to the project, call it EmbeddedResources.cs for the sake of argument. Put the following code in it:

[assembly: WebResource("YourProjectAssemblyNamespace.YourPic.jpg", "image/jpeg")]

namespace YourProjectAssemblyNamespace
{
public class EmbeddedResources
{
}
}


5. Reference the image from your web project like so:

<img src="<%= Page.ClientScript.GetWebResourceUrl(typeof(YourProjectAssemblyNamespace.EmbeddedResources), "YourProjectAssemblyNamespace.YourPic.jpg") %>" />


Woop!

No comments:

Post a Comment

Comments are very welcome but are moderated to prevent spam.

If I helped you out today, you can buy me a beer below. Cheers!