title: Cannot set image source in code behind
body: <p>There are numerous questions and answers regarding setting image source in code behind, such as this <a href="https://stackoverflow.com/questions/350027/setting-wpf-image-source-in-code">Setting WPF image source in code.</a></p>
  
  <p>I have followed all these steps and yet not able to set an image. I code in WPF C# in VS2010. I have all my image files under a folder named "<strong>Images</strong>" and all the image files are set to <strong>Copy always</strong>. And <strong>Build Action</strong> is set to <strong>Resource</strong>, as instructed from documentation.</p>
  
  <p>My code is as follow. I set a <strong>dog.png</strong> in XAML and change it to <strong>cat.png</strong> in code behind.</p>
  
  <pre><code>// my XAML
  &lt;Image x:Name="imgAnimal" Source="Images/dog.png" /&gt;
  
  // my C#
  BitmapImage img = new BitmapImage();
  img.UriSource = new Uri(@"pack://application:,,,/FooApplication;component/Images/cat.png");
  imgAnimal.Source = img;
  </code></pre>
  
  <p>Then I get a empty, blank image of emptiness. I do not understand why .NET makes setting an image so complicated..w</p>
  
  <p><strong>[EDIT]</strong></p>
  
  <p>So the following works</p>
  
  <pre><code>imgAnimal.Source = new BitmapImage(new Uri(@"pack://application:,,,/FooApplication;component/Images/cat.png"));
  </code></pre>
  
  <p>It works, but I do not see any difference between the two code. Why the earlier doesn't work and the latter does? To me they are the same..</p>