Developer forum

Forum » CMS - Standard features » Retrieve image dimension in template?

Retrieve image dimension in template?

Jacob Storgaard Jensen
Reply

Hi Guys,

Is there any way to retrieve the image dimensions (width and height) in a template? I've got this new "lightbox" thing that I would like to implement, but it needs the dimensions of the original image to work. (http://photoswipe.com)

Could it be done with a custom tag extension? Or Razor?


Replies

 
Mikkel Ricky
Reply
This post has been marked as an answer

You can use the Dynamicweb.Image class for this:

@{
    var imageUrl = GetString("Item.Image"); // e.g. "/Files/Images/image.jpg";
    var imagePath = System.Web.HttpContext.Current.Server.MapPath(imageUrl);
    var dimensions = Dynamicweb.Image.GetDimensions(imagePath);
    <pre>@imagePath
@dimensions.X
@dimensions.Y</pre>
}

You can also use System.Drawing.Image.FromFile directly (Dynamicweb.Image.GetDimensions uses this internally).

Best regards,
Mikkel

 

Votes for this answer: 1
 
Jacob Storgaard Jensen
Reply

Razor... It's kind of getting better and better :-D

Thanks!

 

You must be logged in to post in the forum