title: How do you pass a BitmapImage from a background thread to the UI thread in WPF?
body: <p>I have a background thread that generates a series of <code>BitmapImage</code> objects. Each time the background thread finishes generating a bitmap, I would like to show this bitmap to the user. The problem is figuring out how to pass the <code>BitmapImage</code> from the background thread to the UI thread.</p>
  
  <p>This is an MVVM project, so my view has an <code>Image</code> element:</p>
  
  <pre><code>&lt;Image Source="{Binding GeneratedImage}" /&gt;
  </code></pre>
  
  <p>My view-model has a property <code>GeneratedImage</code>:</p>
  
  <pre><code>private BitmapImage _generatedImage;
  
  public BitmapImage GeneratedImage
  {
      get { return _generatedImage; }
      set
      {
          if (value == _generatedImage) return;
          _generatedImage= value;
          RaisePropertyChanged("GeneratedImage");
      }
  }
  </code></pre>
  
  <p>My view-model also has the code that creates the background thread:</p>
  
  <pre><code>public void InitiateGenerateImages(List&lt;Coordinate&gt; coordinates)
  {
      ThreadStart generatorThreadStarter = delegate { GenerateImages(coordinates); };
      var generatorThread = new Thread(generatorThreadStarter);
      generatorThread.ApartmentState = ApartmentState.STA;
      generatorThread.IsBackground = true;
      generatorThread.Start();
  }
  
  private void GenerateImages(List&lt;Coordinate&gt; coordinates)
  {
      foreach (var coordinate in coordinates)
      {
          var backgroundThreadImage = GenerateImage(coordinate);
          // I'm stuck here...how do I pass this to the UI thread?
      }
  }
  </code></pre>
  
  <p>I'd like to somehow pass <code>backgroundThreadImage</code> to the UI thread, where it will become <code>uiThreadImage</code>, then set <code>GeneratedImage = uiThreadImage</code> so the view can update. I've looked at some examples dealing with the WPF <code>Dispatcher</code>, but I can't seem to come up with an example that addresses this issue. Please advise.</p>