If your using DAMP there is a GetImageCropperUrl helper method.
Here is the code. This should also work for MediaById.
///<summary> /// Returns the url for a given crop name using the built in Image Cropper datatype. /// It accepts a razor dynamic xml property. ///</summary> ///<param name="mediaItem">The mediaItem XML property used in razor. Example: @node.slider.mediaItem. It also accepts the child element of mediaItem like "Image".</param> ///<param name="cropName">Name of crop to get url for.</param> ///<returns>Emtpy string or url.</returns> publicstaticstring GetImageCropperUrl(umbraco.MacroEngines.DynamicXml mediaItem, string cropName) { string cropUrl = string.Empty;
Getting crops on media items using Model.MediaById
I have a media item with an imagecropper on it, trying to get to the crops in multiple different ways, but none are giving me results:
Gives: <crops date="12/05/2011 14:04:23"><crop name="NewsItem" x="0" y="0" x2="958" y2="717" url="/media/2154/brouwerwebsite_NewsItem.jpg" /></crops>
That seems good, as it can access the property! But...
Gives: 'string' does not contain a definition for 'crops' (same for: .crop.crop without the "s")
Gives: 'umbraco.MacroEngines.DynamicMedia' does not contain a definition for 'XPath'
And last I tried:
@using umbraco.MacroEngines @{ var mediaItem = (DynamicXml) Model.MediaById(Media.ImagePicker).crop; }
But that gives me: Cannot convert type 'string' to 'umbraco.MacroEngines.DynamicXml'
I'm out of ideas, help?
If your using DAMP there is a GetImageCropperUrl helper method.
Here is the code. This should also work for MediaById.
Jeroen
Not using damp though. But interesting approach, I can probably modify it to make it work!
Well that was quicker than I thought, I use this now:
And your function turned has been put in my App_Code folder (filename: UmbracoHelper.cshtml, hence the namespace above)
Credits should go to uCompontens since that's what my GetImageCropperUrl method is based on: http://ucomponents.codeplex.com/SourceControl/changeset/view/77624#1458752
Only difference is my method is optimized for DAMP and Razor. You can probably use that method now that I think about it...
Jeroen
is working on a reply...