title: DrawingContext.PushOpacityMask() with BitmapImage does not work
body: <p>I want to take a given BitmapImage and use its grayscale/black-and-white representation as an opacity mask on a DrawingContext.</p>
  
  <p>I got far enough to do the color conversion, so I ended up with the follwing state of a demo application:</p>
  
  <p>Method on ViewModel:</p>
  
  <pre><code>public void Demo()
      {
          var width = 400;
          var height = 400;
  
          var target = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
          var target2 = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
  
          var drawingVisual = new DrawingVisual();
          var drawingVisual2 = new DrawingVisual();
  
          using (var drawingContext = drawingVisual.RenderOpen())
          {
              var bitmapImage = new BitmapImage(new Uri(@"pack://application:,,,/WarningFilled.png"));
  
              using (var drawingContext2 = drawingVisual2.RenderOpen())
              {
                  drawingContext2.DrawImage(bitmapImage, new Rect(0, 0, width, height));
              }
  
              target2.Render(drawingVisual2);
  
              var mask = target2.ToGrayscale();
              this.Mask = mask;
  
              drawingContext.PushOpacityMask(new ImageBrush(mask));
              drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0, 0, width, height));
              // drawingContext.Pop();
          }
  
          target.Render(drawingVisual);
  
          this.Rendered = target;
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:wpfApplication1="clr-namespace:WpfApplication1"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Window.DataContext&gt;
      &lt;wpfApplication1:MainViewModel&gt;&lt;/wpfApplication1:MainViewModel&gt;
  &lt;/Window.DataContext&gt;
  &lt;Grid&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="Auto" /&gt;
          &lt;ColumnDefinition Width="Auto" /&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Border Grid.Column="0" BorderThickness="2" BorderBrush="Blue" HorizontalAlignment="Center"&gt;
          &lt;Image Source="{Binding Rendered}" Width="400" Height="400"&gt;&lt;/Image&gt;
      &lt;/Border&gt;
      &lt;Border Grid.Column="1" BorderThickness="2" BorderBrush="Green" HorizontalAlignment="Center"&gt;
          &lt;Image Source="{Binding Mask}" Width="400" Height="400"&gt;&lt;/Image&gt;
      &lt;/Border&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>Running the demo application produces this:
  <img src="https://i.stack.imgur.com/3PCOj.png" alt="enter image description here"></p>
  
  <p>I expected the red rectangle on the left to reflect the opacity mask pushed onto the DrawingContext before drawing the actual rectangle.</p>
  
  <p>The image you see on the right is the BitmapImage after the color conversion, right before it is pushed as an opacity mask. As I mentioned before I also tried it with the black/white conversion, but even with only two colors there's no effect.</p>
  
  <p>I've tried many similar setups and scenarios, but I don't get the opacity mask to work.</p>