Display Image from Media Library in Umbraco 7

扶醉桌前 提交于 2019-12-02 22:12:38

I found this way easy and clean:

@if (CurrentPage.Image != null && !(CurrentPage.Image is Umbraco.Core.Dynamics.DynamicNull))
{
    var m = Umbraco.Media(CurrentPage.Image);

    <img src="@m.Url" alt="@m.UrlName" />
}

I hope that it helps somebody else

Thanks Jesus Mogollon,

I've collapsed that to:

<img src="@Umbraco.Media(CurrentPage.headerBackgroundImage).Url" alt="">

I've set the file to mandatory so hopefully I wont need the if statement part.

Having had such an unexpectedly hard time for something that I would have thought would be easy, this snipped worked for me.

@if (Model.Content.HasValue("OtherImages"))
{
   var otherImages = Model.Content.GetPropertyValue<List<IPublishedContent>>("OtherImages");
   foreach (var image in otherImages)
   {
      if (image != null)
      {
         <img src="@image.Url" alt="@image.Name" class="img-responsive img-rounded"  />
      }
   }
}

Much of the other postings did not work for me, but I think that the API has changed a bit. I'm using Umbraco 7.6.1. I'm not sure whether the null check is necessary, but it certainly won't do any harm.

This worked for me, from the Umbraco 6 Media docs, here

var bannerImage = Umbraco.TypedMedia(Model.Content.GetPropertyValue("plainImage"));

<div class="my-banner-wrapper" style="background-image: url(@bannerImage.GetPropertyValue("umbracoFile"));">
    <!-- some irrelevant content -->
</div>

Try this

@if (CurrentPage.Image != null && !(CurrentPage.Image is Umbraco.Core.Dynamics.DynamicNull))
{
    var m = Umbraco.Media((int)CurrentPage.Image);

    <img src="@m.Url" alt="@m.UrlName" />
}

note: You should cast CurrentPage.Image as int because of Umbraco.Media ambigous constructor

 <img src="@Umbraco.Media(Convert.ToString(@Umbraco.Field("image"))).umbracoFile" alt="" />

Inside the template

umbraco:Macro runat="server" language="cshtml"

img src="@Model.MediaById(Model.photo).umbracoFile" alt=""/

/umbraco:Macro

---Model.photo =photo is a alice name

I entered this into the section above the doctype in the template or master being used.

@{
Layout = null;
var regionalPage = Umbraco.Content(this.CurrentPage.Id);
string manangerPhotoUrl = string.Format("https://assets.yourdomain.com{0}", @Umbraco.Media(regionalPage.managerPhoto).Url);
}

Then I added the variable holding the string value to my image source attribute in the markup.

<img class="img-responsive" src="@manangerPhotoUrl" />
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!