VisualTreeHelper.GetDrawing Method (Visual)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Returns the drawing content of the specified Visual.

Namespace:   System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)

public static DrawingGroup GetDrawing(
	Visual reference


Type: System.Windows.Media.Visual

The Visual whose drawing content is returned.

Return Value

Type: System.Windows.Media.DrawingGroup

The drawing content of the Visual returned as a DrawingGroup type.

The following example shows how to retrieve the DrawingGroup from a visual object by using the GetDrawing method. A hit test is then performed on the geometry of each drawing in the DrawingGroup to determine which geometry was hit.

// Determine if a geometry within the visual was hit.
static public void HitTestGeometryInVisual(Visual visual, Point pt)
    // Retrieve the group of drawings for the visual.
    DrawingGroup drawingGroup = VisualTreeHelper.GetDrawing(visual);
    EnumDrawingGroup(drawingGroup, pt);

// Enumerate the drawings in the DrawingGroup.
static public void EnumDrawingGroup(DrawingGroup drawingGroup, Point pt)
    DrawingCollection drawingCollection = drawingGroup.Children;

    // Enumerate the drawings in the DrawingCollection.
    foreach (Drawing drawing in drawingCollection)
        // If the drawing is a DrawingGroup, call the function recursively.
        if (drawing.GetType() == typeof(DrawingGroup))
            EnumDrawingGroup((DrawingGroup)drawing, pt);
        else if (drawing.GetType() == typeof(GeometryDrawing))
            // Determine whether the hit test point falls within the geometry.
            if (((GeometryDrawing)drawing).Geometry.FillContains(pt))
                // Perform action based on hit test on geometry.


.NET Framework
Available since 3.0
Return to top