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.

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is this the "official" way of fetching and displaying images from the database?
Yes. If you are using ASP.NET WebForms consider using an <asp:Image> tag, but regardless, the result is an <img> tag with a reference to an ASP.NET page that fetches and returns the image content in the DB.
-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.

9 Comments

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?
If you gave your private images a filename of random characters, e.g. RTWQ2312.jpg, then how would these be found?
Random Characters or not, won't they be stored in a folder? can't they access this folder?
They can't list the contents of the folder. Unless you've got your permissions set up incorrectly on your web server...
There are both pros and cons to storing images in a db vs file system. Anybody who claims one way is right is wrong. stackoverflow.com/questions/2517752/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.