title: Missing images in FlowDocument saved as XPS document
body: <p>I am having some difficulties getting images contained in a FlowDocument to show when the FlowDocument is saved as an XPS document.  </p>
  
  <p>Here is what I do:</p>
  
  <ol>
  <li>Create an image using the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.image(VS.95).aspx" rel="noreferrer">Image</a> control of WPF. I set the image source bracketed by calls to BeginInit/EndInit.</li>
  <li>Add the image to the FlowDocument wrapping it in a <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.blockuicontainer.aspx" rel="noreferrer">BlockUIContainer</a>.</li>
  <li>Save the FlowDocument object to an XPS file using a modified version of <a href="http://blogs.msdn.com/fyuan/archive/2007/03/10/convert-xaml-flow-document-to-xps-with-style-multiple-page-page-size-header-margin.aspx" rel="noreferrer">this code</a>.</li>
  </ol>
  
  <p>If I then view the saved file in the XPS viewer, the image is not shown. The problem is that the images are not loaded until actually shown on the screen by WPF so they are not saved to the XPS file.  Hence, there is a workaround: If I first show the document on screen using the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentpageviewer.aspx" rel="noreferrer">FlowDocumentPageViewer</a> and then save the XPS file afterwards, the image is loaded and shows up in the XPS file. This works even if the FlowDocumentPageViewer is hidden. But that gives me another challenge.  Here is what I wish to do (in pseudocode):</p>
  
  <pre><code>void SaveDocument()
  {
      AddFlowDocumentToFlowDocumentPageViewer();
      SaveFlowDocumentToXpsFile();
  }
  </code></pre>
  
  <p>This of course does not work since the FlowDocumentPageViewer never gets a chance to show its contents before the document is saved to the XPS file. I tried wrapping SaveFlowDocumentToXpsFile in a call to Dispatcher.BeginInvoke but it did not help.</p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Can I somehow force the images to load before saving the XPS file without actually showing the document on screen? (I tried fiddling with <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.createoptions(v=VS.90).aspx" rel="noreferrer">BitmapImage.CreateOptions</a> with no luck).</li>
  <li>If there is no solution to question #1, is there a way to tell when FlowDocumentPageViewer has finished loading its contents so that I know when it is save to create the XPS file?</li>
  </ol>