0

I am retrieving an image from SQL Server to display on a new page in my ASP.NET 4.0 application using this method (one of my posts):

How to display an image based on SelectedValue in a GridView?

I am then displaying the image in a new window using this method: (one of my posts):

How to display an image in a new window

What I am trying to do is get the height and width of the image so that the new window is created with the same height and width as the stored image. I guess there are two ways of doing this:

  1. Create a height and width column in my database and store the values when the image is inserted into the database
  2. Retrieve the image from the database and calculate somehow the height and width just before displaying it and then resize the browser window to the right size.

I already have over 150 images in the database so that makes option 1 difficult because I would have to go back over my db and calculate height and width for images that are already in the db. Therefore I would prefer to use option 2, or maybe there's another option? How would I go about doing this?

Thanks for looking.

1
  • 2
    I don't understand how getting the dimensions of 150 images is supposed to be "difficult", since you're just going to have to do it anyways. Commented Nov 7, 2010 at 10:05

2 Answers 2

6

Something like:

System.Drawing.Image image = System.Drawing.Image.FromStream( 
    new System.IO.MemoryStream((byte[]) SqlReader["img_data"]) 
);

int width = image.Width;
int height = image.Height;
Sign up to request clarification or add additional context in comments.

1 Comment

+1. BEST DONE ON SAVING - store with and height in the database, otherwise performance and memory requirements will suck (as you need to decode the image - which is what this above does - for every request just to get width and height).
1

Writing a small program to fetch all the images from the database is going to be much more efficient in the long run than parsing the image data on each request for a page that needs to display an image. Bear in mind that the page itself wouldn't normally need to load the image data at all, just include a reference to it. Further, even the request for the actual image would normally just fetch the blob from the database and return it to the client, without any potentially-costly internal loading.

It should take about 20 minutes to write some code to fetch each image in turn as a byte array, load it with the Bitmap(Stream) constructor (using a MemoryStream to wrap the byte array), ask the bitmap for its dimensions, and then write that to the database.

That 20 minutes of work will save you from having to load the image data for every image which is about to be displayed on a page every time you want to display a page. You'd still need to write the code to load the image from the data anyway - but it'll be happening far more often, and at a time when you don't really want it to.

Comments

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.