How to use embedded image in ASP.NET Server Control

O

Oliver Degnan

I have a server control, written in ASP.NET, where I added two images
as "Embedded Resource" to it.
Now, I need to find a way to read the resource and produce an output
stream to have the image be displayed on the web page as a regular
image HTML tag such as "<img src='myResourceImage.gif'>".

Any help is greatly appreciated.

Thanks in advanvce.

Oliver
 
T

Teemu Keiski

Hi,

to serve the image, you need to have custom Httphandler that would get it
from the resource files (image is separate resource from HTML itself, so it
needs to be requested separately). Anyway, you'd get the resource by using
Assembly.GetManifestResourceStream.
For example Andy Smith has following code in his FirstFocus control to get
embedded js file:

using (System.IO.StreamReader reader = new
System.IO.StreamReader(typeof(FirstFocus).Assembly.GetManifestResourceStream(typeof(FirstFocus),
"FirstFocusScript.js"))) {

String script = "<script language='javascript' type='text/javascript'
\r\n<!--\r\n" + reader.ReadToEnd() + "\r\n//-->\r\n</script>";
this.Page.RegisterClientScriptBlock(pageScriptName, script);

}

Because you have image, you of course read it as stream, not as text.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist









I have a server control, written in ASP.NET, where I added two images
as "Embedded Resource" to it.
Now, I need to find a way to read the resource and produce an output
stream to have the image be displayed on the web page as a regular
image HTML tag such as "<img src='myResourceImage.gif'>".

Any help is greatly appreciated.

Thanks in advanvce.

Oliver
 
O

Oliver Degnan

Thank you very much. I was able to acomplish the task with:

hc.Response.ContentType = "image/" + strType;

asm = Assembly.GetExecutingAssembly();
ReadBinaryResource(asm.GetManifestResourceStream(
cImageResPath + strResName), out byImage, out nLen);
hc.Response.OutputStream.Write(byImage, 0, nLen);



Oliver
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top