Opening an Image from SQL database

  • Thread starter Thread starter Benji
  • Start date Start date
B

Benji

I am trying to open an image from a binary field in SQL Server. I
have got a far as making an object which holds the image. I have done
this using an example off the site of microsoft. Now I am trying to
display the image into an image component. The problem is that the
image component wants an image url, unlike a picturebox which takes
the actual image. How can I reference the image object to my image
component? I am still a begener on c#, this is actually my first
project. Some help will really be appreciated. Thanks.
 
Hi Benji,

You have to save the image on a file, this file MUST BE accesible to the
website, after this all you have to do is put the correct url in the Image
object.
Something like this:

string inThisDirectory = Server.MapPath( "/images_from_db"); //convert a
logical path to a physical one
//this get the image from DB and save it on the directory passed as
parameter
//it return the named used.
//NOTE: you should be careful how to name the image to not overwrite another
image, I would suggest use a Guid.NewGuid().ToString() to named it.
string imagename = GetImageFromDBAndSaveItOnFile( inThisDirectory );

Image.ImageUrl = "/images_from_db/" + imagename;


Hope this help,
 
Back
Top