Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default image resources

    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 Guest

  2. Similar Questions and Discussions

    1. MX 7 uses more resources than MX 6.1?
      I administrate a website, that is viewed by 18.000 unique users. Coldfusion MX 6.1 is running on the server. I decided to try Coldfusion MX 7....
    2. Does 802.11b use a lot of resources?
      Have I tried too hard to squeeze usability out of an old computer? I have a Pentium-166 that has been a faithful router & firewall (FreeBSD 5.3...
    3. out of OS resources
      I don't think that this message is fatal since the error code indicates the situation is temporary. ----- Original Message ----- From: "jorge...
    4. web resources
      Hello I'd like to learn asp, but it seems impossible to find free resources. Does anyone knows sites that offer tutorials, sample scripts and a...
    5. apt resources
      Hello all. Can anyone suggest, or would you even recommend at all for that matter, some good non-official apt sources for debs that are not...
  3. #2

    Default Re: image resources

    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: [url]http://aspalliance.com/726[/url]

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    [url]http://blogs.aspadvice.com/joteke[/url]


    "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    news:%23XEVJXP1GHA.4448@TK2MSFTNGP04.phx.gbl...
    > 1] how can images be stored as resources in a dll and
    >
    > 2] how to retrieve them for usage in a custom assembly ?
    >

    Teemu Keiski Guest

  4. #3

    Default Re: image resources

    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
    XYZ.Resources.

    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(),
    "XYZ.Resources.Icon1.jpg");

    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(),
    "XYZ.Resources.Icon1.jpg");
    }
    }

    Then you just access static properties to access your resources.

    Brennan Stehling
    [url]http://brennan.offwhite.net/blog/[/url]


    "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    news:%23XEVJXP1GHA.4448@TK2MSFTNGP04.phx.gbl...
    > 1] how can images be stored as resources in a dll and
    >
    > 2] how to retrieve them for usage in a custom assembly ?
    >

    Brennan Stehling Guest

  5. #4

    Default Re: image resources

    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_odhtKmwHKuto9Ppv4U 7VgziTrNxAtrvyWBq8_A4mRnZzRjVosI0&t=63293579222000 0000">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 Guest

  6. #5

    Default Re: image resources

    I'm not using visual studio ....

    "Teemu Keiski" <joteke@aspalliance.com> wrote in message news:eBHMk9d1GHA.1256@TK2MSFTNGP04.phx.gbl...
    > 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: [url]http://aspalliance.com/726[/url]
    >
    > --
    > Teemu Keiski
    > ASP.NET MVP, AspInsider
    > Finland, EU
    > [url]http://blogs.aspadvice.com/joteke[/url]
    >
    >
    > "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:%23XEVJXP1GHA.4448@TK2MSFTNGP04.phx.gbl...
    >> 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 Guest

  7. #6

    Default Re: image resources

    I've also tried :

    namespace1.Logo.gif
    namespace1.Resources.Logo.gif






    "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:enEkiAo1GHA.4312@TK2MSFTNGP02.phx.gbl...
    > 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_odhtKmwHKuto9Ppv4U 7VgziTrNxAtrvyWBq8_A4mRnZzRjVosI0&t=63293579222000 0000">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 Guest

  8. #7

    Default Re: image resources

    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.

    XYZ.logo.gif

    Brennan Stehling
    [url]http://brennan.offwhite.net/blog/[/url]

    Jon Paal wrote:
    > I've also tried :
    >
    > namespace1.Logo.gif
    > namespace1.Resources.Logo.gif
    >
    >
    >
    >
    >
    >
    > "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:enEkiAo1GHA.4312@TK2MSFTNGP02.phx.gbl...
    > > 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_odhtKmwHKuto9Ppv4U 7VgziTrNxAtrvyWBq8_A4mRnZzRjVosI0&t=63293579222000 0000">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 ?????
    > >
    Brennan Stehling Guest

  9. #8

    Default Re: image resources

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

    Is this assignable in the assembly info file ?


    "Brennan Stehling" <offwhite@gmail.com> wrote in message news:1158180667.113934.195350@i3g2000cwc.googlegro ups.com...
    > 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.
    >
    > XYZ.logo.gif
    >
    > Brennan Stehling
    > [url]http://brennan.offwhite.net/blog/[/url]
    >
    > Jon Paal wrote:
    >> I've also tried :
    >>
    >> namespace1.Logo.gif
    >> namespace1.Resources.Logo.gif
    >>
    >>
    >>
    >>
    >>
    >>
    >> "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:enEkiAo1GHA.4312@TK2MSFTNGP02.phx.gbl...
    >> > 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_odhtKmwHKuto9Ppv4U 7VgziTrNxAtrvyWBq8_A4mRnZzRjVosI0&t=63293579222000 0000">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 Guest

  10. #9

    Default Re: image resources

    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
    change.

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

    Properties\AssemblyInfo.cs
    Resources\icon1.jpg
    Resources\icon2.jpg

    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"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == ''
    ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>8.0.50727</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{9EAB7BCD-B236-48C2-A986-BA0261935041}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>XYZ</RootNamespace>
    <AssemblyName>XYZ</AssemblyName>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
    'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
    'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Design" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Web" />
    <Reference Include="System.Xml" />
    </ItemGroup>
    <ItemGroup>
    <Compile Include="Properties\AssemblyInfo.cs" />
    </ItemGroup>
    <ItemGroup>
    <EmbeddedResource Include="Resources\icon1.jpg" />
    <EmbeddedResource Include="Resources\icon2.jpg" />
    </ItemGroup>
    <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.target s" />
    <!-- To modify your build process, add your task inside one of the
    targets below and uncomment it.
    Other similar extension points exist, see
    Microsoft.Common.targets.
    <Target Name="BeforeBuild">
    </Target>
    <Target Name="AfterBuild">
    </Target>
    -->
    </Project>


    Brennan Stehling
    [url]http://brennan.offwhite.net/blog/[/url]

    Brennan Stehling Guest

  11. #10

    Default Re: image resources

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

    vbc.exe
    /target:library
    /res:Logo.gif
    /out:\bin\Test.dll empty.vb
    /rootnamespace:myproject

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


    ========default.aspx==========
    <%@ Page Language="vb" Debug="True" %>

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

    <asp:literal id="lit1" runat="server"/>


    Jon Paal Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139