When you populate a DrawingVisual with visual content, you are effectively creating a hierarchical collection of Drawing objects, which are accessible by the DrawingVisual's Drawing
property (of type DrawingGroup). Each of these Drawing objects is actually of one of the following types:
- GeometryDrawing
- GlyphRunDrawing
- ImageDrawing
- VideoDrawing
- DrawingGroup
Two of these provide a property or method to get a Geometry. Obviously, GeometryDrawing has a Geometry
property, whereas GlyphRunDrawing has a GlyphRun
property that returns a GlyphRun object, which in turn has a BuildGeometry
method. This method returns a Geometry of the outline of the GlyphRun's text.
A static helper method to create a Geometry from a DrawingGroup may look like this:
public static Geometry CreateGeometry(DrawingGroup drawingGroup)
{
var geometry = new GeometryGroup();
foreach (var drawing in drawingGroup.Children)
{
if (drawing is GeometryDrawing)
{
geometry.Children.Add(((GeometryDrawing)drawing).Geometry);
}
else if (drawing is GlyphRunDrawing)
{
geometry.Children.Add(((GlyphRunDrawing)drawing).GlyphRun.BuildGeometry());
}
else if (drawing is DrawingGroup)
{
geometry.Children.Add(CreateGeometry((DrawingGroup)drawing));
}
}
geometry.Transform = drawingGroup.Transform;
return geometry;
}
You would just pass the value of the DrawingVisual's Drawing
property to this method:
var geometry = CreateGeometry(visual.Drawing);