Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Candaele 1197 posts 2049 karma points
    May 08, 2012 @ 14:25
    Anthony Candaele
    0

    problem getting Damp image in Razor

    Hi,

    I'm using DAMP to store an image on a resear project node. I'm trying to access the image like this:

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
                  
    @{
        DynamicNode featuredResearch @Model.AncestorOrSelf().Descendants("ResearchProject").Where("researchFeatured").First();
    }

    <div class="C00_Clean container">
      <div class="widget-title">Featured Research</div>
      <div id="twitter" class="widget widget-twitter">
          <p>@featuredResearch.GetProperty("researchProjectTitle").Value</p>
          @if (featuredResearch.HasValue("ResearchProjectImage"))
               {
                var imageId featuredResearch.GetProperty("ResearchProjectImage").Value;
                var image Library.MediaById("imageId.mediaItem.ResearchImage.id");
                <img src="/[email protected]&amp;width=200"/>
                }
       </div>
      </div>

    However this does not work, the line:

    var imageId = featuredResearch.GetProperty("ResearchProject.ImagemediaItem.ResearchImage.id").Value;

    results in a null value

    my xml looks like this:

    <ResearchProject id="1080" parentID="1091" level="4" writerID="0" creatorID="0" nodeType="1076" template="1093" sortOrder="1" createDate="2012-04-27T15:40:32" updateDate="2012-05-08T13:30:54" nodeName="Reclamewijsheid bij kinderen en jongeren" urlName="reclamewijsheid-bij-kinderen-en-jongeren" writerName="admin" creatorName="admin" path="-1,1051,1079,1091,1080" isDoc="">

              <promotors>

                <MultiNodePicker type="content">

                  <nodeId>1066</nodeId>

                  <nodeId>1070</nodeId>

                </MultiNodePicker>

              </promotors>

              <externalPromotorName>Prof. dr. Patrick De Pelsmacker</externalPromotorName>

              <externalPromotorEmail>[email protected]</externalPromotorEmail>

              <researchSummary><![CDATA[De gevoeligheid van kinderen voor reclame en de vraag of het ethisch verantwoord is om reclame te richten naar kinderen is al jaren onderwerp van maatschappelijke en politieke discussies. ]]></researchSummary>

              <researchFeatured>1</researchFeatured>

              <researchProjectTitle>Reclamewijsheid bij kinderen en jongeren</researchProjectTitle>

              <researchProjectImage>

                <DAMP fullMedia="">

                  <mediaItem>

                    <ResearchImage id="1120" version="4bdb892e-38ca-495b-9039-7475d068ea1e" parentID="1118" level="3" writerID="0" nodeType="1117" template="0" sortOrder="1" createDate="2012-05-08T13:30:42" updateDate="2012-05-08T13:30:42" nodeName="clp_kleur_centre" urlName="clp_kleur_centre" writerName="admin" nodeTypeAlias="ResearchImage" path="-1,1111,1118,1120">

                      <umbracoFile>/media/1272/clp_kleur_centre.jpg</umbracoFile>

                      <umbracoWidth>425</umbracoWidth>

                      <umbracoHeight>198</umbracoHeight>

                      <umbracoBytes>12124</umbracoBytes>

                      <umbracoExtension>jpg</umbracoExtension>

                    </ResearchImage>

                  </mediaItem>

                </DAMP>

              </researchProjectImage>

    ...

    Does anyone knows how I can get to the image in Razor?

    Thanks for your help,

    Anthony

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 08, 2012 @ 15:01
    Tom Fulton
    1

    Hi Anthony,

    Since you're storing the Full Media XML (option in DAMP prevalue editor), you don't need to mess with getting the Id of the image.  You can just do this:

    @if (featuredResearch.HasValue("ResearchProjectImage"))
    {
        var image = featuredResearch.ResearchProjectImage.mediaItem.ResearchImage.umbracoFile;
        <img src="/imageGen.ashx?image=@image&amp;width=200"/>
    HTH,
    Tom 
  • Anthony Candaele 1197 posts 2049 karma points
    May 08, 2012 @ 15:15
    Anthony Candaele
    0

    Hi Tom,

    I have tried your solution, but VS doesn't recognize the object 'ResearchProjectImage' in 

    var image = featuredResearch.researchProjectImage.mediaItem.ResearchImage.umbracoFile;

    The VS Intellisense says it cannot resolve the symbol researchProjectImage

    could this be because featuredResearch is of type DynamicNode ?

    Thanks for your help,

    Anthony

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 08, 2012 @ 15:20
    Tom Fulton
    1

    Yes, you won't get intellisense for any custom properties.  You can change from DynamicNode to just dynamic to get around the warning, but then you don't get the intellisense with the available methods.

    Does the code work or are you getting an error on the front end also?

  • Anthony Candaele 1197 posts 2049 karma points
    May 08, 2012 @ 15:24
    Anthony Candaele
    0

    Hi Tom,

    No the code doesn't work, I'm getting an error on the front end

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 08, 2012 @ 15:30
    Tom Fulton
    1

    Hmm, well maybe in order to access custom properties you can't use DynamicNode.  I've not tried using it before so not sure if that's the case.

    As a workaround you can change your first line to:

    var featuredResearch = @Model.AncestorOrSelf().Descendants("ResearchProject").Where("researchFeatured").First();
    (or)
    dynamic featuredResearch = @Model.AncestorOrSelf().Descendants("ResearchProject").Where("researchFeatured").First();

    Or you can cast it to dynamic when you make your call to the property:

    var image = ((dynamic)featuredResearch).ResearchProjectImage.mediaItem.ResearchImage.umbracoFile;

    Normally you can also use .GetProperty but I don't think this casts to DynamicXML or DataTypeModels like the Model.property notation does.

    HTH,
    Tom

  • Anthony Candaele 1197 posts 2049 karma points
    May 08, 2012 @ 15:33
    Anthony Candaele
    0

    Hi Tom,

    I changed the type of the variable 'featuredResearch' to var and now it works!

    thanks a lot,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    May 08, 2012 @ 15:36
    Anthony Candaele
    0

    If it can help someone else working with DAMP and Razor, this is the full code:

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
                  
    @{
        var featuredResearch @Model.AncestorOrSelf().Descendants("ResearchProject").Where("researchFeatured").First();
    }

    <div class="C00_Clean container">
      <div class="widget-title">Featured Research</div>
      <div id="twitter" class="widget widget-twitter">
          <p>@featuredResearch.GetProperty("researchProjectTitle").Value</p>
          @if (featuredResearch.HasValue("researchProjectImage"))
               {
                var image featuredResearch.researchProjectImage.mediaItem.ResearchImage.umbracoFile;
                <img src="/imageGen.ashx?image=@image&amp;width=200"/>
               }
       </div>
      </div>

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    May 08, 2012 @ 15:47
    Jeroen Breuer
    1

    Hi Anthony,

    Thanks for sharing your code. I see that you're already using a custom media type for ResearchImage, but you're still using ImageGen to get the correct size. This is a good combination, but have you considered using the Image Cropper for this? Than you have more control over the output of the image. In this video I explain how it works and what the benefits are: http://www.youtube.com/watch?v=LkcObL7CMvQ

    Jeroen

  • Anthony Candaele 1197 posts 2049 karma points
    May 08, 2012 @ 16:15
    Anthony Candaele
    0

    Hi Jeroen,

    Thanks for the information, I have watched the video, but to be honest, I don't fully get the concept of a crop. I mean, except for having more control over the output of an image, are there any other advantages with using crops over resizing images in code using imageGen ?

    I use DAMP because it gives me more control about which media can be stored in wich media folders, as this enhances the experience of the end users using a CMS. I didn't yet create custom media folders though, only custom media types. Maybe I should create also custom media folders so I can control which type of media can be stored in a given media folder.

    I must say, DAMP is a great package, lately I created a video gallery using FlowPlayer and DAMP to select videos. It worked out really well and I will blog about it in the near future.

    greetings,

    Anthony

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    May 08, 2012 @ 17:28
    Jeroen Breuer
    0

    The advantage of a crop is that if you give it a height and width it will always have that size. If you have multiple images which all have a diffent width and height they will still look the same. For example this website uses DAMP for the photo gallery. As you can see the width and height is always good because it's cropped. If the user wants a different part of the image to be visible he can go to the media section and drag the crop to select it.

    I also use custom media folders for the same reason you described. If you use my custom version of MFU you can also only select the media types which are allowed.

    Jeroen

  • Anthony Candaele 1197 posts 2049 karma points
    May 08, 2012 @ 20:37
    Anthony Candaele
    0

    Hi Jeroen,

    Thanks for your explanation. So you mean a crop has always the same width and height, wherever it's used, while an image resized with imageGen only has the appropriate width and height where it is defined in the code?

    I have used imageGen for this video gallery to resize all images to a same width and height (see screenshot below)

     

    the code for this video-gallery looks like this:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines
    @{
        var videos @Model.Children.Where("Visible");    
    }
    @foreach (var itemgroup in videos.InGroupsOf(3))
      {    
            <div style="float:left;clear:right;margin-right:10px;margin-left:10px;margin-top:10px;margin-bottom:5px;">
            @foreach (var video in itemgroup)
            {
               
               var videothumbnail video.videoThumbnail.mediaItem.VideoImage;              
               <div style="float:left;margin-left:10px;margin-top:10px;margin-bottom:5px;">
                 <href="@video.Url">
                  <img src="/[email protected]&amp;width=200&amp;height=150" style="margin-right:10px;margin-bottom:5px;" />
                 </a>
                 <p><strong>@video.videoTitle</strong><br />
                 Length@video.videoDuration</p>
               </div>
                                   
            }
            </div>   
    }

    So I don't know why I should use crops instead of just resizing the thumbnails with imageGen. The only advantage of using crops in this particular case is that the user has some more control over the output of the thumbnails.

    greetings,

    Anthony

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    May 18, 2012 @ 15:08
    Jeroen Breuer
    1

    If you use a crop in this example the images will look better. Now they are all stretched. The original images probably look better and if you use crops these images will look the same as the original images.

    Jeroen

  • Anthony Candaele 1197 posts 2049 karma points
    May 18, 2012 @ 15:52
    Anthony Candaele
    0

    Hi Jeroen,

    Thanks for the tip, I will try it out and let you know.

    greetings,

    Anthony

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Oct 31, 2012 @ 18:21
    Jeroen Breuer
    0

    I've released the DAMP Razor Model package. It's much easier to use DAMP in Razor now. Here's a video: http://www.screenr.com/qvj8.

    Jeroen

  • Gordon Saxby 1444 posts 1855 karma points
    Nov 13, 2012 @ 12:59
    Gordon Saxby
    0

    I have installed the DAMP Razor Model and am trying to use it in Visual Studio.

    I have a pretty simple setup - DAMP datatype set to "Full media xml" and only one image can be selected. All I want to do is get the URL of the selected image. Could someone show me how I would do that?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Nov 13, 2012 @ 13:04
    Jeroen Breuer
    0

    Here is an example which should work if you have the DAMP Razor Model installed:

    @{
    dynamic media = Model.dampProperty.First;
    <img src="@media.Url" alt="@media.Alt"/>

    Jeroen

  • Gordon Saxby 1444 posts 1855 karma points
    Nov 13, 2012 @ 13:20
    Gordon Saxby
    0

    Umm, I have the line

            dynamic headerImage = Model.HeaderImage.First;

    but I get the error

    'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'First'

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Nov 13, 2012 @ 13:22
    Jeroen Breuer
    0

    Hmm that means the DAMP Razor Model is not working. What version of Umbraco are you using and which package did you install? Do you have the DAMP.RazorModel.dll in your bin folder?

    Jeroen

  • Gordon Saxby 1444 posts 1855 karma points
    Nov 13, 2012 @ 13:58
    Gordon Saxby
    0

    Umbraco 4.9.1

    Downloaded / Installed DAMP_Razor_Model_1.2.zip

    The DAMP.RazorModel.dll file is in the BIN folder. Do I need to add a reference to it in Visual Studio?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Nov 13, 2012 @ 14:05
    Jeroen Breuer
    0

    No it should just work. Do you have a special version of DAMP? When you look at the DAMP datatype what is the GUID and the render control?

    Jeroen

  • Gordon Saxby 1444 posts 1855 karma points
    Nov 13, 2012 @ 14:18
    Gordon Saxby
    0

    the GUID is ef94c406-9e83-4058-a780-0375624ba7ca - according to the "Installed Packages" section it is version 2.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Nov 13, 2012 @ 14:21
    Jeroen Breuer
    0

    Hmm everything should just work. Could you please show your complete Razor file here? Perhaps than I can see if there is something wrong.

    Jeroen

  • Gordon Saxby 1444 posts 1855 karma points
    Nov 13, 2012 @ 14:27
    Gordon Saxby
    0

    There must be something going on elsewhere - I have stripped it down to

    @{
    dynamic headerImage = Model.HeaderImage.First;
    }

    and it still gives an error!?

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Nov 13, 2012 @ 14:35
    Jeroen Breuer
    0

    Yes the problem is that the DAMP Razor Model isn't working. Because it's not working it returns DynamicXml (which is defeault) instead of the Model I wrote for it. I really don't know why it's not working. Could you perhaps download the source and debug to see if that works? Just download the latest version here: http://damp.codeplex.com/SourceControl/list/changesets It's the DAMP.RazorModel project. You can try if the ModelBinder Init method is hit.

    uComponents also has some Razor models. You could try to see if that works.

    Finally you could try to just use a clean solution and see if that works. You can watch a video on how to do that here: http://www.screenr.com/qvj8

    Jeroen

     

  • Gordon Saxby 1444 posts 1855 karma points
    Nov 13, 2012 @ 15:19
    Gordon Saxby
    0

    Sorry ... I don't have the time to spend on it right now :-(

    I have got it working by using:

    headerImageUrl = Model.HeaderImage.mediaItem[0].Image.umbracoFile;

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Nov 13, 2012 @ 18:02
    Jeroen Breuer
    0

    Too bad it didn't work. If you've got time you could also look in log table to see if there might be an error causing this. 

    Just thought about one last thing which could be problem. If you added some references to dll's in your Visual Studio project which aren't in the bin folder this can also break it. See this topic: http://our.umbraco.org/forum/developers/api-questions/24461-Events-not-being-fired#comment90973

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft