Try our conversational search powered by Generative AI!

How to get image dimensions?



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

Nov 17, 2016 15:18


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.

Edited, Nov 17, 2016 22:05

That worked, thanks! :)

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.