Loading an image webcontrol directly without saving file to server

M

Mark Denardo

My question is similar to one someone posted a few months back, but I don't
see any replies.

Basically I want to be able to have users upload photos and save them in a
database (as byte data) and be able to load them to an image webcontrol, but
system.web.ui.webcontrols.image only seems to have a control to load the
image from a URL.

There's no way to load this directly without saving the image as a file and
then using "Image1.ImageUrl = ..."?

Seems a little silly, am I missing something obvious here?



----- Original Message -----
From: "byrd48" <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Sent: Wednesday, July 05, 2006 9:02 PM
Subject: system.drawing.image to system.web.ui.webcontrols.image
 
M

Mark Fitzpatrick

You can only load the image from a URL. This is more a limitation in HTML
than anything else. There's no way to embed the blob directly into the page.
The image tag can only refer to another file. In this case, your best bet is
to create a web page that will load the appropriate image. Then your image
url points to that web page in order to generate a valid image from it (of
course, passing some id in the querystring so the page knows which image to
retrieve).
 
G

Guest

In answer to your question, get your bytes from the database, write them to a
MemoryStream, and get the image using Image.FromStream().

Of course, there are "almost" no cases where I would recommend storing the
images in a database rather than to the file system and storing paths in the
database but that's another topic.

Dale
 
G

Guest

One thing I forgot, and it sort of adds to what Mark replied. Once you have
your image object, use the Save(Stream, ImageType) method to write your image
to the Response.OutputStream object.

So, as Mark said, you set the src of your IMG tag to, for instance
"createImage.aspx" and the createImage.aspx codebehind generates the image
and sends it like this:

Image img = GetImage() // or however you get your image
Response.Clear();
img.Save(Response.OutputStream, ImageType.JPEG);
Response.Flush();
Response.End();


HTH

Dale
 
L

Laurent Bugnion

Hi,

Mark said:
You can only load the image from a URL. This is more a limitation in HTML
than anything else. There's no way to embed the blob directly into the page.
The image tag can only refer to another file. In this case, your best bet is
to create a web page that will load the appropriate image. Then your image
url points to that web page in order to generate a valid image from it (of
course, passing some id in the querystring so the page knows which image to
retrieve).

That sounds like a job for an ASHX custom handler rather than for a page.

Greetings,
Laurent
 
M

Mark Denardo

Ok, thanks everyone for the input. Keeping the file on disk and saving the
path to the file in the database was my first idea, but I was having problem
refreshing the image if the user changed the photo, etc. I guess I need
resolve that issue rather than keeping the image in memory. I'll open up a
new thread.
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top