November Happy Hour will be moved to Thursday December 5th.

How to get image dimensions?

Vote:
 

Hi,

Is there a way to get the image dimensions from an uploaded image? The property is of type ContentReference with ImageFile as UIHint.

Regards, Torunn

#171775
Nov 17, 2016 15:18
Vote:
 

Hi,

I don't have the code infront of me now. But first you need to load the MediaData object from the ContentReference you have then load the binary data into an Image, something like this:

var mediaData = GetMediaDataFromContentReferenceSomehow(yourContentReference);

using (var image = Image.FromStream(mediaData.BinaryData.OpenRead()))
{
   var height = image.Height;
   var width = image.Width;
}

This might be an expensive operation to do, so keep that in mind. Cache the result or add dimension properties to you content data model and set the values after the image is uploaded by listening to content events. That way you don't have to load the binary data every time and the data will be cached.

#171813
Edited, Nov 17, 2016 22:05
Vote:
 

That worked, thanks! :)

#171818
Nov 18, 2016 9:09
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.