title: Creating a CroppedBitmap at runtime - won't load from resource
body: <p>I'm trying to write code that will load an image from a resource, and then crop it. This code works when I do all, or part, of it in XAML. I want to switch from all-XAML to all-code, so I can reuse this more than one place, with different Uris.</p>
  
  <p>But when I try to do the same thing in code, I get a DirectoryNotFoundException, because suddenly it starts trying to look for a folder on disk, instead of loading the image from the resource.</p>
  
  <ul>
  <li>If I load the BitmapImage in XAML, and then create a CroppedBitmap in XAML, everything works.</li>
  <li>If I load the BitmapImage in XAML, and then write code to create a CroppedBitmap from it, everything works.</li>
  <li>If I load the BitmapImage in code, <em>without</em> creating a CroppedBitmap from it, everything works.</li>
  <li>But if I load the BitmapImage in code <em>and</em> create a CroppedBitmap in code, it tries to load from the filesystem instead of the resources, and I get a DirectoryNotFoundException.</li>
  </ul>
  
  <p>Code samples are below. I'm sure I'm doing something stupid, but I've run through the whole thing three times now (once in my real app, once in a test app, and once while writing up this question), and I got the same results all three times.</p>
  
  <p>For all of the following code samples, I've created an Images folder inside my project, and added an existing image there called "elf.png", with properties set to defaults (Build Action = "Resource"; Copy to Output Directory = "Do not copy").</p>
  
  <hr>
  
  <p>Case 1: Both BitmapImage and CroppedBitmap in XAML.</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication8.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="Window1" Height="300" Width="300"&gt;
      &lt;Window.Resources&gt;
          &lt;BitmapImage x:Key="fullImage" UriSource="Images/elf.png"/&gt;
          &lt;CroppedBitmap x:Key="croppedImage" Source="{StaticResource fullImage}"
                         SourceRect="0 0 240 320"/&gt;
      &lt;/Window.Resources&gt;
      &lt;Image Source="{StaticResource croppedImage}"/&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>This shows the cropped portion of the bitmap, as expected.</p>
  
  <hr>
  
  <p>Case 2: BitmapImage in XAML; CroppedBitmap in code-behind.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication8.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="Window1" Height="300" Width="300"&gt;
      &lt;Window.Resources&gt;
          &lt;BitmapImage x:Key="fullImage" UriSource="Images/elf.png"/&gt;
      &lt;/Window.Resources&gt;
      &lt;Image Name="image"/&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Constructor in code-behind:</p>
  
  <pre><code>public Window1()
  {
      InitializeComponent();
      var fullImage = (BitmapImage) FindResource("fullImage");
      var croppedImage =
          new CroppedBitmap(fullImage, new Int32Rect(0, 0, 240, 320));
      image.Source = croppedImage;
  }
  </code></pre>
  
  <p>This also shows the cropped portion of the bitmap, as expected.</p>
  
  <hr>
  
  <p>Case 3: BitmapImage in code; no CroppedBitmap.</p>
  
  <pre><code>public Window1()
  {
      InitializeComponent();
      var uri = new Uri("Images/elf.png", UriKind.RelativeOrAbsolute);
      var fullImage = new BitmapImage(uri);
      image.Source = fullImage;
  }
  </code></pre>
  
  <p>This shows the entire bitmap. This isn't what I want, but does tell me that I know how to write C# code to create the right kind of Uri and load a BitmapImage from a resource.</p>
  
  <hr>
  
  <p>Case 4: BitmapImage and CroppedBitmap in code.</p>
  
  <pre><code>    public Window1()
      {
          InitializeComponent();
          var uri = new Uri("Images/elf.png", UriKind.RelativeOrAbsolute);
          var fullImage = new BitmapImage(uri);
          var croppedImage =
              new CroppedBitmap(fullImage, new Int32Rect(0, 0, 240, 320));
          image.Source = croppedImage;
      }
  </code></pre>
  
  <p>As far as I can tell, this just puts together the same pieces as before. It uses code that I know will load a BitmapImage from a resource, and code that I know will crop a section from a loaded BitmapImage. But somehow, when the two are put together, it forgets that the resource is there, and tries to load from disk. I get the following exception:</p>
  
  <ul>
  <li>XamlParseException: "Cannot create instance of 'Window1' defined in assembly 'WpfApplication8, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation.  Error in markup file 'Window1.xaml' Line 1 Position 13."
  
  <ul>
  <li><em>Inner exception:</em> TargetInvocationException: "Exception has been thrown by the target of an invocation."
  
  <ul>
  <li><em>Inner exception:</em> DirectoryNotFoundException: "Could not find a part of the path 'C:\svn\WpfApplication8\WpfApplication8\bin\Debug\Images\elf.png'."</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>The inner-inner-exception stack trace shows that the original exception (the DirectoryNotFoundException) is being thrown by the line that instantiates the CroppedBitmap. I don't know why that line would be trying to read from disk, or why it doesn't work when the as-far-as-I-can-tell-equivalent XAML works fine.</p>
  
  <p>Since I know the XAML is using the parameterless constructors, I also tried the following version, which should be much closer to what the XAML actually does:</p>
  
  <pre><code>public Window1()
  {
      InitializeComponent();
      var uri = new Uri("Images/elf.png", UriKind.RelativeOrAbsolute);
      var fullImage = new BitmapImage();
      fullImage.BeginInit();
      fullImage.UriSource = uri;
      fullImage.EndInit();
      var croppedImage = new CroppedBitmap();
      croppedImage.BeginInit();
      croppedImage.Source = fullImage;
      croppedImage.SourceRect = new Int32Rect(0, 0, 240, 320);
      croppedImage.EndInit();
      image.Source = croppedImage;
  }
  </code></pre>
  
  <p>Same exception, this time from the <code>croppedImage.EndInit();</code> line.</p>
  
  <p>Any ideas on how I can get the all-code version to correctly load the resource and crop the image? What's happening in the XAML version that's different?</p>