When using DrawingContext.DrawRectangle with a TileBrush, I noticed that the top-left of the rectangle is not the top-left of the underlying image. This occurs with both a DrawingBrush and an ImageBrush with the source as a DrawingImage. Is there a way to force DrawRectangle
to always snap the underlying brush to the top-left corner? Here's a picture:
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:
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));
}
* In this case an ImageBrush would give the correct results, but in my program, I'm dealing with a DrawingBrush with a custom GeometryDrawing.
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.