views:

543

answers:

5

Hi

I save my images into my SQL Server Database with ASP.NET(2.0).

(imageData -> image) (imageType -> varchar) (imageLength -> bigint)

Thus the imageData will be "Binary data" and the imageType will be like "image/gif" and the imageLength will be like "6458".......

Is it possible to get the image HEIGHT and WIDTH from my VB.NET code inside my ASP.NET? I want to make my picture box on my web form the size of the actual image that is saved in my database.

Regards Etienne

A: 

Sounds like you want 2 more fields in your database, height and width.

Nick Veys
Cant i get that data from my Binary data all ready?
Etienne
Well sure, if you reconstruct the image there are Height and Width attributes on it. I guess I assumed otherwise.
Nick Veys
+3  A: 

I would save the height and width of the image in separate columns when you save the image to the database intially. Then when you do your select statement to read the image out of the database, you can also access the height and width fields from the database.

Alternatively you can access the height and width information when you load the image out of the database and then set the height and width properties before assigning the image to the picture box.

KevB
+6  A: 

Assuming you have the data in a stream:

System.Drawing.Image.FromStream(yourStream).Height

You are probally better doing this when you save the image to the DB, as I'm sure that loading the image object isn't going to be cheap.

Edit

If we take this to email then the next guy with this issue won't have a record of our solution. Let's keep it in the forum for now.

Just so we know, I am a C# developer so I'm not going to try and remember vb.net syntax if this is an issue and you need help converting let me know.

You have an IDataReader I'm assuming which is pulling an Image or binary varbinary etc field from your DB. You need to load it into an object which derives from System.IO.Stream. For our purposes a MemoryStream is the perfect choice as it doesn't require a backing store such as a disk.

System.IO.MemoryStream yourStream = new System.IO.MemoryStream(dr["imgLength"] as byte[]);
System.Drawing.Image yourImage=System.Drawing.Image.FromStream(yourStream);

yourImage.Height;
yourImage.width
JoshBerke
Awsome glad to hear! good luck
JoshBerke
+2  A: 

You can get the height and width but you are going to have to load the whole image into memory using the system.drawing.image library, each time you need that info.

Better to save it as separate fields the first time you save it to the database, that is generally what I do.

EJB
A: 

I strongly believe that after a few hundred gigabytes of images you'll find yourself thinking that the file system and static file http servers are better suited than the databas for storing images. It also allows you to use thousands of existing free tools to work with, move, host, etc the images. Your database might get busy, and it's not easy to cluster.

dlamblin