VisualTreeHelper.GetDrawing Method

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

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft