Partilhar via


Como: enumerar o conteúdo de desenho de um visual

O objeto Drawing fornece um modelo de objeto para enumerar o conteúdo de um Visual.

Exemplo

O exemplo a seguir usa o método GetDrawing para recuperar o valor DrawingGroup de um Visual e enumerá-lo.

Observação

Ao enumerar os conteúdos do elemento visual, está a recuperar objetos Drawing e não a representação subjacente dos dados de renderização como uma lista de instruções de gráficos vetoriais. Para obter mais informações, consulte Visão geral da renderização de gráficos do WPF.

public void RetrieveDrawing(Visual v)
{
    DrawingGroup drawingGroup = VisualTreeHelper.GetDrawing(v);
    EnumDrawingGroup(drawingGroup);
}

// 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 is DrawingGroup group)
        {
            EnumDrawingGroup(group);
        }
        else if (drawing is GeometryDrawing)
        {
            // Perform action based on drawing type.
        }
        else if (drawing is ImageDrawing)
        {
            // Perform action based on drawing type.
        }
        else if (drawing is GlyphRunDrawing)
        {
            // Perform action based on drawing type.
        }
        else if (drawing is VideoDrawing)
        {
            // Perform action based on drawing type.
        }
    }
}

Ver também