This documentation is archived and is not being maintained.

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
)
You cannot use methods in XAML.

Parameters

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.
            }
        }

    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5, 3.0
Show: