如何获得DrawingVisual区域

时间:2012-05-03 20:51:43

标签: .net wpf drawing

我有DrawingVisual这样:

Rect MyRect = new Rect(new Point(0, 0), new Size(100, 100));
DrawingVisual MyVisual = new DrawingVisual();

using (DrawingContext context = MyVisual.RenderOpen()) {
context.DrawRectangle(Brushes.Black, new Pen(), MyRect);
context.PushTransform(new TranslateTransform(50, 50));
context.PushTransform(new ScaleTransform(2, 2));
}

我想得到描述元素区域的Geometry,在本例中为RectangleGeometry,其中Rect属性为:

Rect(new Point(50, 50), new Size(200, 200))

感谢。

1 个答案:

答案 0 :(得分:3)

如果你在绘制Rect之前推动变换,你可以通过ContentBounds属性获得适当的界限:

Rect rect = new Rect(new Size(100, 100));

using (DrawingContext dc = visual.RenderOpen())
{
    dc.PushTransform(new TranslateTransform(50, 50));
    dc.PushTransform(new ScaleTransform(2, 2));
    dc.DrawRectangle(Brushes.Black, null, rect);
}

System.Diagnostics.Trace.TraceInformation("Bounds = {0}", visual.ContentBounds);

来自PushTransform的备注部分:

  

转换适用于所有后续绘图命令,直到它为止   被Pop命令删除。