DrawingVisual.Drawing Property

 
System_CAPS_noteNote

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

Gets the drawing content of the DrawingVisual object.

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

public DrawingGroup Drawing { get; }

Property Value

Type: System.Windows.Media.DrawingGroup

Gets a value of type DrawingGroup that represents the collection of Drawing objects in the DrawingVisual.

A visual object stores its rendering data as a vector graphics instruction list. Each item in the instruction list represents a low-level set of graphics data in a serialized format. For more information on how visual objects store graphics data, see WPF Graphics Rendering Overview.

Any change to the returned DrawingGroup value is not persisted by the DrawingVisual.

In the following example, the DrawingGroup value of each DrawingVisual in a host visual is enumerated.

// Enumerate the DrawingVisual children of a host visual.
public void RetrieveDrawings(Visual visualHost)
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visualHost); i++)
    {
        // Retrieve child visual at specified index value.
        DependencyObject dependencyObject = VisualTreeHelper.GetChild(visualHost, i);

        // Determine if the child object is a DrawingVisual.
        if (dependencyObject.GetType() == typeof(DrawingVisual))
        {
            DrawingVisual drawingVisual = (DrawingVisual)dependencyObject;

            if (drawingVisual.Drawing != null)
            {
                if (drawingVisual.Drawing.GetType() == typeof(DrawingGroup))
                {
                    // Enumerate the drawings in the DrawingGroup.
                    EnumDrawingGroup(drawingVisual.Drawing);
                }
            }
        }
     }
 }

 // Enumerate the drawings in the DrawingGroup.
 public void EnumDrawingGroup(DrawingGroup drawingGroup)
 {
     DrawingCollection dc = drawingGroup.Children;

     // Enumerate the drawings in the DrawingCollection.
     foreach (Drawing drawing in dc)
     {
         // If the drawing is a DrawingGroup, call the function recursively.
         if (drawing.GetType() == typeof(DrawingGroup))
         {
             EnumDrawingGroup((DrawingGroup)drawing);
         }

         if (drawing.GetType() == typeof(GeometryDrawing))
         {
             // Perform action based on drawing type.
         }
     }
 }

.NET Framework
Available since 3.0
Return to top
Show: