是否有人知道是否可以将DrawingContext
保存或转换为Geometry
?
E.g。之后
using (DrawingContext dc = RenderOpen())
{
dc.DrawLine(penSelected, Data.MidTop, Data.MidTop + vertical);
dc.DrawLine(pen, Data.MidTop - horizontal, Data.MidTop + thickness);
dc.DrawLine(pen, Data.MidTop + vertical - thickness, Data.MidTop + horizontal + vertical);
dc.DrawText(new FormattedText(Data.Time2.ToString("0.0"), cultureinfo, FlowDirection.LeftToRight, typeface, 8, Brushes.Black),
Data.MidTop + 3 * thickness);
dc.DrawText(new FormattedText(Data.Time2.ToString("0.0"), cultureinfo, FlowDirection.LeftToRight, typeface, 8, Brushes.Black),
Data.MidTop + vertical - horizontal - 3 * thickness);
}
以某种方式将绘制的对象保存在几何体中?
答案 0 :(得分:8)
当您使用可视内容填充DrawingVisual时,您实际上正在创建Drawing对象的分层集合,这些对象可由DrawingVisual的Drawing
属性(DrawingGroup类型)访问。这些Drawing对象中的每一个实际上都是以下类型之一:
其中两个提供了获取几何的属性或方法。显然,GeometryDrawing具有Geometry
属性,而GlyphRunDrawing具有GlyphRun
属性,该属性返回GlyphRun对象,而该对象又具有BuildGeometry
方法。此方法返回GlyphRun文本轮廓的几何。
从DrawingGroup创建Geometry的静态帮助器方法可能如下所示:
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;
}
您只需将DrawingVisual的Drawing
属性的值传递给此方法:
var geometry = CreateGeometry(visual.Drawing);