title: How to reference image source files that are packaged with my metro style app?
body: <p>I have a .png file as a content of my application. When I bind it in xaml like this</p>
  
  <pre><code>&lt;ImageBrush x:Key="BtnBackImageBrush" ImageSource="/Assets/Images/back.png" /&gt;
  </code></pre>
  
  <p>everything ok.</p>
  
  <p>I read <a href="http://msdn.microsoft.com/en-us/library/windows.ui.xaml.controls.image.source.aspx">this article</a> and when I try to get access to this .png programmatically I get an error.</p>
  
  <p>Code I use:</p>
  
  <pre><code>Uri baseUri = new Uri("ms:appx");
  Image img = new Image();
  img.Source = new BitmapImage(new Uri(baseUri, "/Assets/Images/back.png"));
  img.ImageFailed += (sender, args) =&gt; new MessageDialog("Error").ShowAsync();
  </code></pre>
  
  <p>And my question is how to reference image source files that are packaged with my metro style app?</p>
  
  <p>Thanks for advices.</p>
  
  <p><strong>UPDATE:</strong>
  I found answer!
  We need set baseUri using parent FrameworkElement instead of setting it manually. For example:</p>
  
  <pre><code>// Usage
  myImage.Source = ImageFromRelativePath(this, "relative_path_to_file_make_sure_build_set_to_content");
  
  public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path)
  {
      var uri = new Uri(parent.BaseUri, path);
      BitmapImage result = new BitmapImage();
      result.UriSource = uri;
      return result;
  }
  </code></pre>
  
  <p>Thanks for this <a href="http://www.codingbeta.com/programatically-setting-image-source-in-metro-style-apps-with-wpf/">article</a>.</p>