1

我有一个像这样的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));
}

我想获得描述元素区域的几何,在这种情况下是RectangleGeometry,其中 Rect 属性是:

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

谢谢。

4

1 回答 1

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 命令删除。

于 2012-05-04T05:09:18.060 回答