can't display resource image from assembly

J

Jon Paal

sample code which is failing to display the image



rootnamespace.........

myproject





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

but it does not display in the web page.


by using reflector I can see the image is stored in the DLL as a resource and is correctly named


What am I missing ????
 
A

Alex

Hi, Jon

Every thing seems to be fine. I have similar code that is working... May be
you'll find it usefull.

= = = = = = = = = = = = = = = = = = = =
HandlerTest.cs
= = = = = = = = = = = = = = = = = = = =
using System.Web;
using System.Web.UI;
using System.Web.Configuration;
using System;

[assembly: WebResource("img2.gif", "image/gif")]
namespace HandlerExample
{
public class MyHttpHandler : System.Web.UI.Page, IHttpHandler
{
// Override the ProcessRequest method.
public void ProcessRequest(HttpContext context)
{
String img2 = String.Format("<img src={0}>",
Page.ClientScript.GetWebResourceUrl(this.GetType(),
"img2.gif"));

context.Response.Write(img2);
}


// Override the IsReusable property.
public bool IsReusable
{
get { return true; }
}
}
}

= = = = = = = = = =
Command line
= = = = = = = = = =
csc /t:library /out:../Bin/HandlerTest.dll /r:System.Web.dll
/res:img2.gif,img2.gif HandlerTest.cs


= = = = = = = = = =
Web.Config registration
= = = = = = = = = =
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="handler.aspx"
type="HandlerExample.MyHttpHandler,HandlerTest"/>
</httpHandlers>
</system.web>
</configuration>


Regards,
Alex
 
J

Jon Paal

thanks!

I got the image to appear, but it did require 2 changes to match your coding;

1] the .vb file had to be compiled into the dll to work, can't be in the app_code folder
2] the httphandlers had to be in the web.config file.


Is there a way to have the httphandlers added programatically to the web.config file ?
 
A

Alex

Hi, Jon
I got the image to appear, but it did require 2 changes to match your
coding;
1] the .vb file had to be compiled into the dll to work, can't be in the
app_code folder

Have no idea how to solve issue with App_Code, but may be such approach can
be usefull for you:
1) Compile your images into a .dll with SomeClass.
2) Then use that SomeClass from the code inside App_Code.

= = = = = = = = = = = = = = = = = = = =
HandlerTestRes.cs
= = = = = = = = = = = = = = = = = = = =
using System.Web.UI;

[assembly: WebResource("img1.gif", "image/gif")]
namespace HandlerExample
{
public class SomeClass
{
}
}

= = = = = = = = = =
Command line
= = = = = = = = = =
csc /t:library /out:../Bin/HandlerTestRes.dll /res:img1.gif,img1.gif
HandlerTestRes.cs

= = = = = = = = = = = = = = = = = = = =
SomePage.aspx.cs
= = = = = = = = = = = = = = = = = = = =
using System;
using System.Web;

public partial class SomePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String img1 = String.Format("<img src={0}>",
Page.ClientScript.GetWebResourceUrl(typeof(HandlerExample.SomeClass),
"img1.gif"));

Response.Write(img1);
}
}

2] the httphandlers had to be in the web.config file.

Are you writing the HttpHandler, or just an ASPX page? If ASPX (and you code
located in corresponding .aspx.vb file) then you don't need the httphandlers
section in web.config.
Is there a way to have the httphandlers added programatically to the
web.config file ?
Don't think so.

Regards,
Alex.
 
J

Jon Paal

thanks again it is now working, seems my earlier problems with app_code were namespace related.


Alex said:
Hi, Jon
I got the image to appear, but it did require 2 changes to match your coding;
1] the .vb file had to be compiled into the dll to work, can't be in the app_code folder

Have no idea how to solve issue with App_Code, but may be such approach can be usefull for you:
1) Compile your images into a .dll with SomeClass.
2) Then use that SomeClass from the code inside App_Code.

= = = = = = = = = = = = = = = = = = = =
HandlerTestRes.cs
= = = = = = = = = = = = = = = = = = = =
using System.Web.UI;

[assembly: WebResource("img1.gif", "image/gif")]
namespace HandlerExample
{
public class SomeClass
{
}
}

= = = = = = = = = =
Command line
= = = = = = = = = =
csc /t:library /out:../Bin/HandlerTestRes.dll /res:img1.gif,img1.gif HandlerTestRes.cs

= = = = = = = = = = = = = = = = = = = =
SomePage.aspx.cs
= = = = = = = = = = = = = = = = = = = =
using System;
using System.Web;

public partial class SomePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String img1 = String.Format("<img src={0}>",
Page.ClientScript.GetWebResourceUrl(typeof(HandlerExample.SomeClass),
"img1.gif"));

Response.Write(img1);
}
}

2] the httphandlers had to be in the web.config file.

Are you writing the HttpHandler, or just an ASPX page? If ASPX (and you code located in corresponding .aspx.vb file) then you
don't need the httphandlers section in web.config.
Is there a way to have the httphandlers added programatically to the web.config file ?
Don't think so.

Regards,
Alex.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top