locked
Absolute Image Paths vs Relative RRS feed

  • Question

  • I had a usercontrol that had images in it.  I moved it to a different folder and it broke because the image paths were no longer valid:

    Old Path:

    images/myimage.jpg

     

    New Path:

    ../images/myimage.jpg

     How can I reference the image so it is always correct?  In asp.net I would use the ~ ... is there some way to make the image a resource so the reference never changes even if the user control is moved?

     

    Monday, January 12, 2009 11:03 AM

Answers

  • One way is to set the Image file's Build Action to "Content" so that it is placed inside you XAP file at build time. If you do that, then you would always reference the image file as Source="/myimage.jpg" (with a / prefix)

    Otherwise (if you do not want them packaged in the XAP), the image Url should always be relative to the ClientBin directory in your web folder. So if you have myimage.jpg in your /ClientBin folder of your web project, you can reference it as Source="myimage.jpg" (no forward slash)

    It's kind of confusing but there is a good post here of the different options - http://blogs.msdn.com/katriend/archive/2008/04/27/different-ways-for-loading-images-and-files-in-silverlight-2-applications.aspx

     

    Monday, January 12, 2009 11:12 AM

All replies

  • One way is to set the Image file's Build Action to "Content" so that it is placed inside you XAP file at build time. If you do that, then you would always reference the image file as Source="/myimage.jpg" (with a / prefix)

    Otherwise (if you do not want them packaged in the XAP), the image Url should always be relative to the ClientBin directory in your web folder. So if you have myimage.jpg in your /ClientBin folder of your web project, you can reference it as Source="myimage.jpg" (no forward slash)

    It's kind of confusing but there is a good post here of the different options - http://blogs.msdn.com/katriend/archive/2008/04/27/different-ways-for-loading-images-and-files-in-silverlight-2-applications.aspx

     

    Monday, January 12, 2009 11:12 AM
  •  And what if you want to reference an image that's *not* in ClientBin? Using ".." doesn't seem to work (at least when giving URIs to UriSource for bitmaps for images).

    Monday, March 30, 2009 3:10 PM
  • You would need to give an absolute Url, which you could form using the DocumentUri property of HtmlDocument. You can use something like the function below to form an absolute Uri for a resource ...

    public static string GetUrlForResource(string resourcePage)
    {
        string webUrl = System.Windows.Browser.HtmlPage.Document.DocumentUri.ToString();

     

        string containerPage = webUrl.Substring(webUrl.LastIndexOf("/") + 1);    webUrl = webUrl.Replace(containerPage, resourcePage);     return webUrl;

    }

     

    Monday, March 30, 2009 5:30 PM