title: Silverlight : BitmapImage to WriteableBitmap
body: <p>I can successfully load the following Bitmap like this and display it within an Image control on the view.</p>
  
  <pre><code>var bitmapImage = new BitmapImage
                              {
                                  UriSource = 
                                      new Uri("../Images/Test.JPG", UriKind.Relative)
                              };
  </code></pre>
  
  <p>However as soon as I add this line to create a WriteableBitmap out of the bitmap, </p>
  
  <pre><code>    var w = new WriteableBitmap(bitmapImage);
  </code></pre>
  
  <p>I get a Runtime error at the line above: "Object reference not set to an instance of an object."</p>
  
  <p>It seems the BitmapImage creation is delayed, could that be? How should I fix this?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I am now trying this but the openImage seems never to be hit. (even without trying to make it synchronous, it still fails) What is wrong here?</p>
  
  <pre><code>var image = new BitmapImage();
          image.ImageOpened += (sender, args) =&gt; resetEventBitmap.Set();
          image.ImageFailed += (o, eventArgs) =&gt;
                                   {
                                       resetEventBitmap.Set();
                                       throw eventArgs.ErrorException;
                                   };
          image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
          image.UriSource = uri;
  
          resetEventBitmap.WaitOne();
  </code></pre>
  
  <p>Thanks,</p>