show image stored in database

M

Mike P

I have images in my database that are stored as bytes, and I want to
show them on screen on my webpage. I can do this with the code below :

Response.Clear();
Response.ContentType = "image/jpeg";

System.Drawing.Image image = retrieveImage(strEncodedImage);
image.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);

But this always clears what is on the rest of the webpage. How can I do
this whilst keeping the rest of the page intact?
 
A

Alberto Poblacion

Mike P said:
I have images in my database that are stored as bytes, and I want to
show them on screen on my webpage. I can do this with the code below :

Response.Clear();
Response.ContentType = "image/jpeg";

System.Drawing.Image image = retrieveImage(strEncodedImage);
image.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);

But this always clears what is on the rest of the webpage. How can I do
this whilst keeping the rest of the page intact?

You need to insert an <img ...> control inside the page that you wish to
contain the image. The "src" of the img control would then point to the
..aspx (or better yet, .ashx) that contains the code that you have posted
above.
 
W

Wyrm

I have images in my database that are stored as bytes, and I want to
show them on screen on my webpage. I can do this with the code below :

Response.Clear();
Response.ContentType = "image/jpeg";

System.Drawing.Image image = retrieveImage(strEncodedImage);
image.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);

But this always clears what is on the rest of the webpage. How can I do
this whilst keeping the rest of the page intact?

*** Sent via Developersdexhttp://www.developersdex.com***

Your problem here is that you save the image to the webpage
outputstream, thus overwriting your webpage content in the response.

You should create a HttpHandler as source for an Image control on your
webpage.
You will save your image as this handler's Response.OutputStream, just
as you're doing here.
Then you'll just have to pass your "strEncodedImage" as parameter when
using this Handler in the ImageUrl property.

Something like this:

public class Handler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
Image image = retrieveImage(context.Request.QueryString
["strEncodedImage"]);
context.Response.ContentType = "image/jpeg";
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}

private Image retrieveImage(string strEncodedImage)
{
// Your code here
}

public bool IsReusable
{
get
{
return false;
}
}
}

Then add this to your page:

<asp:Image ID="imageFromDB" ImageUrl="~/Handler.ashx?strEncodedImage=
[name of image here]" runat="server" />
 

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

Similar Threads


Top