如何:枚举 Visual 的绘图内容

更新:2007 年 11 月

Drawing 对象提供了用来枚举 Visual 内容的对象模型。

示例

下面的示例使用 GetDrawing 方法来检索 VisualDrawingGroup 值并枚举该值。

说明:

您在枚举可视化层的内容时,就是相当于在检索 Drawing 对象,而不是以向量图形指令列表形式检索呈现数据的基础表示。有关更多信息,请参见 Windows Presentation Foundation 图形呈现概述

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

}

 // 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);
         }
         else if (drawing.GetType() == typeof(GeometryDrawing))
         {
             // Perform action based on drawing type.  
         }
         else if (drawing.GetType() == typeof(ImageDrawing))
         {
             // Perform action based on drawing type.
         }
         else if (drawing.GetType() == typeof(GlyphRunDrawing))
         {
             // Perform action based on drawing type.
         }
         else if (drawing.GetType() == typeof(VideoDrawing))
         {
             // Perform action based on drawing type.
         }
     }
 }

请参见

概念

Drawing 对象概述

Windows Presentation Foundation 图形呈现概述

参考

Drawing

DrawingGroup

VisualTreeHelper