Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting image height and width when image is saved in database

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

like image 971
Etienne Avatar asked Jan 27 '26 21:01

Etienne


1 Answers

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
like image 192
JoshBerke Avatar answered Jan 29 '26 10:01

JoshBerke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!