title: Crop/copy stretched image in WPF
body: <p>I want to build a small image and text editor.
  I have two <code>Image</code>s in a <code>UserControl</code>.</p>
  <p>On the left side is the editor and looks momentarily (thanks to some try and error) a little bit overstated, but it works nonetheless. (I will clean up the code later)</p>
  <pre><code>&lt;Canvas x:Name=&quot;EditCanvas&quot; Grid.Column=&quot;0&quot;
          Height=&quot;{Binding AreaHeight}&quot; Width=&quot;{Binding AreaWidth}&quot;
          MaxWidth=&quot;426&quot;
          MouseLeftButtonDown=&quot;EditCanvas_MouseLeftButtonDown&quot;&gt;
      &lt;Canvas.Background&gt;
          &lt;VisualBrush TileMode=&quot;Tile&quot; Viewport=&quot;0, 0, 1, 1&quot; Stretch=&quot;{Binding SelectedStretch}&quot;&gt;
              &lt;VisualBrush.Visual&gt;
                  &lt;Image x:Name=&quot;EditorImage&quot;
                         Height=&quot;{Binding AreaHeight}&quot; Width=&quot;{Binding AreaWidth}&quot;
                         Source=&quot;{Binding FileImagePath}&quot; Margin=&quot;1&quot;/&gt;
              &lt;/VisualBrush.Visual&gt;
          &lt;/VisualBrush&gt;
      &lt;/Canvas.Background&gt;
  &lt;/Canvas&gt;
  </code></pre>
  <p>Here I bind the <code>Image</code> to a <code>UriSource</code>. The stretching will change the <code>Image</code> itself, not the underlying source. Now I want to crop this image 'as it is'! So, when it is set to <code>Stretch.UniformToFill</code> or <code>Stretch.Fill</code> (for example) I want the cropped image to look exactly as it is shown in the left area. That means, the cropped image can be cropped on the bottom, the right, not at all or something like this. The formula to calculate this by myself would be a little bit greater.
  Now I wonder if there isn't a better way. I have to draw text later on to this cropped image, so I cannot simply convert it to a <code>DrawingContext</code>, <code>DrawingVisual</code>, and so on and draw text in it, because that would also stretch the text later on the right side.
  And if possible it would be nice to get a fast method, that can be redrawn a few times per second without killing the GPU.</p>
  <p>For showing the preview image I use this XAML at the moment:</p>
  <pre><code>&lt;Canvas Grid.Column=&quot;1&quot;
          Height=&quot;{Binding AreaHeight}&quot; Width=&quot;{Binding AreaWidth}&quot;
          MaxWidth=&quot;426&quot;&gt;
      &lt;Canvas.Background&gt;
          &lt;VisualBrush TileMode=&quot;Tile&quot;
                          Viewport=&quot;0, 0, 1, 1&quot;
                          Stretch=&quot;{Binding SelectedStretch}&quot;&gt;
              &lt;VisualBrush.Visual&gt;
                  &lt;Image Height=&quot;{Binding AreaHeight}&quot; Width=&quot;{Binding AreaWidth}&quot;
                          Source=&quot;{Binding PreviewImage}&quot;
                          Margin=&quot;1&quot; x:Name=&quot;TestImage&quot;/&gt;
              &lt;/VisualBrush.Visual&gt;
          &lt;/VisualBrush&gt;
      &lt;/Canvas.Background&gt;
  &lt;/Canvas&gt;
  &lt;Border Grid.Column=&quot;1&quot;
          BorderThickness=&quot;1&quot; BorderBrush=&quot;Black&quot;
          Height=&quot;{Binding AreaHeight}&quot; Width=&quot;{Binding AreaWidth}&quot; /&gt;
  </code></pre>
  <p>And this code behind to calculate the image and fill it with the desired text (even if it is faulted at this moment):</p>
  <pre><code>BitmapSource imageSource = EditorImage.Source as BitmapSource;
  DrawingVisual drawingVisual = new DrawingVisual();
  
  using (DrawingContext drawingContext = drawingVisual.RenderOpen())
  {
      drawingContext.DrawImage(imageSource, new Rect(0, 0, imageSource.PixelWidth, imageSource.PixelHeight));
  
      List&lt;TextPosition&gt; textPositions = GetTextPositions();
  
      if (textPositions.Any())
      {
          textPositions.ForEach(x =&gt;
              drawingContext.DrawText(new FormattedText(x.Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                  new Typeface(x.FontFamily, x.Style, x.Weight, FontStretch), x.FontSize, new SolidColorBrush(x.ForegroundColor),
                  VisualTreeHelper.GetDpi(drawingVisual).PixelsPerDip), x.Position)
          );
      }
  }
  
  PreviewImage = new RenderTargetBitmap(imageSource.PixelWidth, imageSource.PixelHeight, 96, 96, PixelFormats.Pbgra32);
  PreviewImage.Render(drawingVisual);
  TestImage.InvalidateVisual();
  </code></pre>