title: WPF TileBrush and DrawingContext.DrawRectangle -- relative to upper-left of rectangle?
body: <p>When using <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.drawingcontext.aspx" rel="nofollow noreferrer">DrawingContext</a>.<a href="http://msdn.microsoft.com/en-us/library/ms606813.aspx" rel="nofollow noreferrer">DrawRectangle</a> with a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.tilebrush.aspx" rel="nofollow noreferrer">TileBrush</a>, I noticed that the top-left of the rectangle is not the top-left of the underlying image. This occurs with both a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.drawingbrush.aspx" rel="nofollow noreferrer">DrawingBrush</a> and an <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.imagebrush.aspx" rel="nofollow noreferrer">ImageBrush</a> with the source as a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.drawingimage.aspx" rel="nofollow noreferrer">DrawingImage</a>. Is there a way to force <code>DrawRectangle</code> to always snap the underlying brush to the top-left corner? Here's a picture:</p>
  
  <p><img src="https://i.stack.imgur.com/6GAJa.png" alt="An image of the brush alignment issues"></p>
  
  <p>The two rectangles are using the same brush but are at different points on the screen. As you can see, the brush's origin is different (it seems the brush is continuous over a much larger area). Here's a minimal repro:</p>
  
  <pre><code>    private static readonly Brush _brush;
  
      static CustomControl()
      {
          Uri uri = new Uri(@"pack://application:,,,/WpfApplication1;component/image.png", UriKind.Absolute);
          BitmapImage img = new BitmapImage(uri);
          Rect rect = new Rect(0, 0, img.Width, img.Height);
          ImageDrawing drawing = new ImageDrawing(img, rect);
          _brush = new DrawingBrush
          {
              Drawing = drawing, 
              Viewport = rect,
              ViewportUnits = BrushMappingMode.Absolute,
              TileMode = TileMode.Tile 
          };
          _brush.Freeze();
      }
  
      protected override void OnRender(DrawingContext dc)
      {
          dc.DrawRectangle(_brush, null, new Rect(70, 70, 100, 150));
          dc.DrawRectangle(_brush, null, new Rect(200, 200, 80, 120));
      }
  </code></pre>
  
  <p><sub>* In <em>this</em> case an ImageBrush would give the correct results, but in my program, I'm dealing with a DrawingBrush with a custom <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.geometrydrawing.aspx" rel="nofollow noreferrer">GeometryDrawing</a>.</sub></p>
  
  <p>I've tried using a pen instead of a brush, changing the TileMode, setting the stretch to Uniform, changing AlignmentX and AlignmentY... nothing seems to work. The origin of the brush is never set to the origin of the rectangle.</p>