Rendering Media
Info on rendering media items and imaging cropping
Rendering a media item
Example 1: Accessing a Media Image IPublishedContent item based on the ID
@{
// The Umbraco Helper has a Media method that will retrieve a Media Item by Guid in the form of IPublishedContent. In this example, the Media Item has a Guid of 55240594-b265-4fc2-b1c1-feffc5cf9571
var mediaItem = Umbraco.Media(Guid.Parse("55240594-b265-4fc2-b1c1-feffc5cf9571"));
if (mediaItem != null)
{
// To get the URL for your media item, you use the Url method:
var url = mediaItem.Url();
// to read a property by alias
var imageHeight = mediaItem.Value<int>("umbracoHeight");
var imageWidth = mediaItem.Value<int>("umbracoWidth");
var orientationCssClass = imageWidth > imageHeight ? "img-landscape" : "img-portrait";
<img src="@url" alt="@mediaItem.Name" class="@orientationCssClass"/>
}
}Example 2: Accessing a Media Image ModelsBuilder item based on the ID
Working with Video files
Example: Rendering a Video Media item
Other Media Items such as File
FileImage Cropper
Example of using Image Cropper
More information
Last updated
Was this helpful?