0

My Table in the Database contains a column with a type : image.

I have managed to upload the image to the database.

But almost all tutorials out there, shows us how to give the Image Controller a URL which contains the name of the page, plus a parameter that has the ID of the selected row in the Database

Example:

//Database codes here... (query..etc..)
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.HasRows == true)
{
    myReader.Read();
    Response.ContentType = "image/jpg";
    Response.BinaryWrite((byte[])myReader[0]);
    Image1.ImageUrl="User.aspx?imgid=1";
}

So when I click on a specific button which should fetch this image from the database, then give that URL to the image, the image opens in full screen.. But I don't want that, let's say I have empty space in my page for an Image of 50px width and 50px height, how can I show the image inside this space? and keeping all the other contents? just like a user page, a profile page.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
user1665700
  • 512
  • 2
  • 13
  • 28
  • This is almost the same, you just not make them thumbnails http://stackoverflow.com/questions/13988269/how-can-i-generate-a-thumbnail-from-an-image-in-server-folder/13988448#13988448 – Aristos Dec 21 '12 at 23:55

2 Answers2

3

You can control the image's display using an <img> tag. So say your ASP.NET page that gets and displays the image is called ShowImage.aspx?ID=xxx.

Create a new page named ShowImageAt50px.aspx and in that page add the following markup:

<img src="ShowImage.aspx?ID=xxx" style="width:50px;" alt="" />

Now when you visit ShowImageAt50px.aspx it will show the image constrained at 50 pixels wide.

Scott Mitchell
  • 8,659
  • 3
  • 55
  • 71
  • Is this the "official" way of fetching and displaying images from the database? – user1665700 Dec 22 '12 at 08:02
  • Yes. If you are using ASP.NET WebForms consider using an tag, but regardless, the result is an tag with a reference to an ASP.NET page that fetches and returns the image content in the DB. – Scott Mitchell Dec 24 '12 at 03:34
-1

It's usually far easier and less error-prone to store your images in the file-system and refer to your images in the database using the filenames of your images.

I've never come across a good reason to store images in a database.

Also, when you want to edit your image that's stored in the database, then you are going to have to export it out of your database and then reimport it back in.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
  • I've divided my Images into 2 Categories. Public images, that are stored in a folder. (Easy to use) Private images, user specific images that I feel that I should upload to the database to prevent other users from simply reaching them. Am I wrong? – user1665700 Dec 21 '12 at 23:45
  • If you gave your private images a filename of random characters, e.g. RTWQ2312.jpg, then how would these be found? – Lee Taylor Dec 21 '12 at 23:47
  • Random Characters or not, won't they be stored in a folder? can't they access this folder? – user1665700 Dec 21 '12 at 23:50
  • They can't list the contents of the folder. Unless you've got your permissions set up incorrectly on your web server... – Lee Taylor Dec 21 '12 at 23:52
  • There are both pros and cons to storing images in a db vs file system. Anybody who claims one way is right is wrong. http://stackoverflow.com/questions/2517752/images-in-database-vs-file-system – Steve's a D Dec 21 '12 at 23:54
  • Then this is another issue, "HOW TO PREVENT ACCESS TO A FOLDER", thank you. – user1665700 Dec 21 '12 at 23:54
  • @LeeTaylor, even if the names are randomized, a problem you could run into is that a user who does have access to the image views one and then saves a link or emails someone the URL purposefully or unintentionally. Now a non-authorized user can view the supposedly private image. – Scott Mitchell Dec 21 '12 at 23:58
  • @ScottMitchell - Agreed, although when I answered originally there was no mention to private/public images. – Lee Taylor Dec 22 '12 at 00:08
  • @user1665700 - I'd suggest "unanswering" my answer, and going with Scott's – Lee Taylor Dec 22 '12 at 00:09