Question

I have a DrawingVisual object I have created for example using this method:

Private Function CreateDrawingVisualRectangle() As DrawingVisual

   Dim drawingVisual As New DrawingVisual() 
   Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
   Dim rect As New Rect(New Point(160, 100), New Size(320, 80))
   drawingContext.DrawRectangle(Brushes.LightBlue, New Pen(Brushes.Black, 0.5), rect)

   Return drawingVisual

End Function

There's a way to get the type of shape drawn by DrawingContext method and its properties?

I.e.:

Dim MyVisual as DrawingVisual = CreateDrawingVisualRectangle()

Dim MyVisualType as MyType = MyVisual.GetDrawingType()
Dim MyBrush as Brush = MyVisual.GetDrawingBrush()
Dim MyPen as Pen = MyVisual.GetDrawingPen()
Dim MyRect as Rect = MyVisual.GetDrawingRect()
...

Obviously the methods I used in the last example is indicative methods that does not exist in reality, but I'd use to get MyVisual properties.

Thanks.

Was it helpful?

Solution

You could recursivly iterate through the Drawing objects in the DrawingGroup provided by the Drawing property of a DrawingVisual and if a child drawing is a GeometryDrawing, check for its Pen, Brush and Geometry properties:

void InspectDrawings(DrawingVisual drawingVisual)
{
    InspectDrawings(drawingVisual.Drawing);
}

void InspectDrawings(DrawingGroup drawingGroup)
{
    foreach (Drawing drawing in drawingGroup.Children)
    {
        if (drawing is GeometryDrawing)
        {
            GeometryDrawing geometryDrawing = (GeometryDrawing)drawing;
            // inspect properties here
        }
        else if (drawing is DrawingGroup)
        {
            // recurse into sub-group
            InspectDrawings((DrawingGroup)drawing);
        }
    }
}

You may now retrieve the type of Geometry and check for more special properties. For example if it is a RectangleGeometry, check its Rect property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top