Sophtware
Sophtware

Reputation: 1824

Umbraco 6.x .Net Control Media from Id

I have a Node with a property for an image from the Media library. The property contains the Id of the Media image. I need to get the Media object from this Id. In Umbraco 4.x you could just do the following:

var image = new Media(imageId);

For some reason, this method seems to be missing in action. The only one I have found is one that will take the name of the image from the Media library. But I only get the Id.

This is for a .Net Control that I will be using on a page. I just need to get the full path to the image so I can use it in the SRC field of the image tag.

Upvotes: 2

Views: 2139

Answers (2)

Eric Herlitz
Eric Herlitz

Reputation: 26267

you can also use the uQuery library that is a part of Umbraco nowadays

Media mediaObject = umbraco.uQuery.GetMedia(mediaId);

Upvotes: 3

Sophtware
Sophtware

Reputation: 1824

I found the answer after digging around in the source. To get a Media object by Id from within a WebControl in version 6.x you have to do the following:

var mediaObject = ApplicationContext.Current.Services.MediaService.GetById(mediaId);

Then you can get the following properties from the object:

var filePath = mediaObject.Properties["umbracoFile"].Value as string;
var width = mediaObject.Properties["umbracoWidth"].Value as int?;
var height = mediaObject.Properties["umbracoHeight"].Value as int?;
var bytes = mediaObject.Properties["umbracoBytes"].Value as int?;
var fileExtension = mediaObject.Properties["umbracoExtension"].Value as string;

Hope this helps someone else out!

Upvotes: 7

Related Questions