Cómo obtener el área de DrawingVisual

Tengo un DibujoVisual Me gusta esto:

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));

Quiero conseguir el Geometría which describes the area of the element, in this case a RectánguloGeometría where Rect property is:

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


preguntado el 03 de mayo de 12 a las 21:05

1 Respuestas

If you would push the transforms before drawing the Rect, you could get the proper bounds by the ContentBounds propiedad:

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);

From the Remarks section in EmpujarTransformar:

The transform applies to all subsequent drawing commands until it is removed by the Pop command.

contestado el 04 de mayo de 12 a las 16:05

seems to work, but there are some cases in my application where there's a strange behavior (the result is {0,0,300,300}). See mi pregunta Por favor. - planeador

Changed the answer to use ContentBounds instead of DescendantBounds. Sorry, but i confused the two. Does that make any difference? - Clemens

I'll try, I have to solve the problem of the other parallel question. - planeador

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.