image resources

Discussion in 'ASP .Net Building Controls' started by Jon Paal, Sep 10, 2006.

  1. Jon   Paal

    Jon Paal Guest

    1] how can images be stored as resources in a dll and

    2] how to retrieve them for usage in a custom assembly ?
    Jon Paal, Sep 10, 2006
    1. Advertisements

  2. Jon   Paal

    Teemu Keiski Guest

    1) In Visual studio, set tbuild action to embedded resource for the image
    file when they are embedded to the dll as resource (note you want to do that
    in a class library project for web resources to work)

    2) Assuming you have reference to the assembly (via Type.GetType etc) then
    by calling Assembly.GetManifestResourceStream is one way. Another is to use
    web resources. See:
    Teemu Keiski, Sep 11, 2006
    1. Advertisements

  3. What you need to do is place your files into a class library. Set the
    Build Action property for the file you want to embed as an Embedded
    Resource. This will cause it to be included in the assembly.

    Next you need tag it with AssembyInfo.cs. Assume you have a default
    namespace of XYZ and place your embedded files into a folder called
    Resources. The namespace for that folder will automatically be

    In your AssemblyInfo.cs you need to add reference to it.

    [assembly: WebResource("XYZ.Resources.Icon1.jpg", "image/jpeg")]

    That will make it available as a web resource. Now you want to get the Url
    to access the content.

    string url = Page.ClientScript.GetWebResourceUrl(GetType(),

    Notice it calls GetType(). That is used to tell it which assembly to use to
    load the resource. You may want to place a dummy class in your assembly and
    use it as a reference here. You may even want to provide a class called
    ResourceLoader.cs and give it useful Properties.

    public static string Icon1Url {
    get {
    return Page.ClientScript.GetWebResourceUrl(GetType(),

    Then you just access static properties to access your resources.

    Brennan Stehling
    Brennan Stehling, Sep 12, 2006
  4. Jon   Paal

    Jon Paal Guest

    sample code which is failing to display the image


    Assembly code................
    <Assembly: WebResource("Logo.gif", "image/gif")>

    code ...............
    Imports System
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Web
    Imports System.Web.UI
    Imports Microsoft.VisualBasic

    NameSpace namespace1

    Class class1
    Inherits System.Web.UI.Page
    Implements IHttpHandler

    Public Function showimg() As String

    Dim Response As HttpResponse = HttpContext.Current.Response
    Dim returnValue As String = String.Empty
    returnValue = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "Logo.gif")
    response.write( "<img src=""" & returnValue & """>")

    End Function

    End Class
    End Namespace

    viewsource output for image is ...................

    <img src="/WebResource.axd?d=whMLBJ8IDJGvV_odhtKmwHKuto9Ppv4U7VgziTrNxAtrvyWBq8_A4mRnZzRjVosI0&t=632935792220000000">by using
    reflector I can see the image is stored in the DLL as a resource and is correctly named, but it does not display in the web
    page.What am I missing ?????
    Jon Paal, Sep 12, 2006
  5. Jon   Paal

    Jon Paal Guest

    I'm not using visual studio ....

    Jon Paal, Sep 12, 2006
  6. Jon   Paal

    Jon Paal Guest

    I've also tried :

    Jon Paal, Sep 12, 2006
  7. Your web page will not be in the same assembly as the embedded
    resource. The reference to Me.GetType() will not work for you.

    You need to reference a class which is in the same assembly as the one
    you want to access.

    And both VB.NET and C# in .NET 2.0 have a default namespace, so if you
    set that default root namespace to XYZ, and place your embedded file,
    logo.gif, in the root folder you will use this reference.


    Brennan Stehling
    Brennan Stehling, Sep 13, 2006
  8. Jon   Paal

    Jon Paal Guest

    I don't have/use Visual Studio ...

    Is this assignable in the assembly info file ?
    Jon Paal, Sep 14, 2006
  9. Sorry I did not get back to you sooner. Without a version of Visual
    Studio which can work with class library projects, you will have some
    difficulty. To get around it to create assemblies with embedded
    resources you can instead use MSBuild.

    Below is a modified copy of a C# project file. Changing it from csproj
    to vbproj may be all you need to do to make it a VB project. You can
    use MSBuild to compile this project. Note the ItemGroup listing the
    EmbeddedResource elements. You can edit that to add to your embedded
    resources. I also set the default namespace to XYZ which you can

    What you will need below the folder holding this .csproj file is a
    Properties and Resources folder for these files...


    In AssemblyInfo.cs you will want this code, or for VB.NET you just
    adjust the brackets.

    C# version:
    [Assembly: WebResource("XYZ.Resources.icon1.gif", "image/gif")]
    [Assembly: WebResource("XYZ.Resources.icon2.gif", "image/gif")]

    VB.NET Version:
    <Assembly: WebResource("XYZ.Resources.icon1.gif", "image/gif")>
    <Assembly: WebResource("XYZ.Resources.icon2.gif", "image/gif")>

    # library.csproj

    <Project DefaultTargets="Build"
    <Configuration Condition=" '$(Configuration)' == ''
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
    'Debug|AnyCPU' ">
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
    'Release|AnyCPU' ">
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Design" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Web" />
    <Reference Include="System.Xml" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <EmbeddedResource Include="Resources\icon1.jpg" />
    <EmbeddedResource Include="Resources\icon2.jpg" />
    <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
    <!-- To modify your build process, add your task inside one of the
    targets below and uncomment it.
    Other similar extension points exist, see
    <Target Name="BeforeBuild">
    <Target Name="AfterBuild">

    Brennan Stehling
    Brennan Stehling, Sep 18, 2006
  10. Jon   Paal

    Jon Paal Guest

    Thanks for replying.

    Actually, compiling is the easy part. Here is my solution that seems to work.

    =========run command - adjust physical paths as needed - line breaks for clarity============

    /out:\bin\Test.dll empty.vb

    =========== empty.vb ===========

    Imports System
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Reflection
    Imports System.Web
    Imports System.Web.Configuration
    Imports System.Web.UI

    <Assembly: WebResource("Logo.gif", "image/gif")>

    NameSpace HandlerExample
    Public Class SomeClass
    End Class
    End Namespace

    <%@ Page Language="vb" Debug="True" %>

    <script runat="server">
    Sub Page_Load
    Dim img1 As String = String.Empty
    img1 = Page.ClientScript.GetWebResourceUrl(GetType(myproject.HandlerExample.SomeClass) , "Logo.gif")
    lit1.Text = ( "<img src=""" & img1 & """>")
    End Sub

    <asp:literal id="lit1" runat="server"/>
    Jon Paal, Sep 18, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.