WPF: Converting nested DrawingGroup to nested DrawingVisual
I have a DrawingGroup with nested c开发者_StackOverflow社区hildren created from SVG files and I want to render/convert this to a corresponding nested DrawingVisual/Visual.
A simple rendering process, as follows...
DrawingGroup group; // assuming root group of diagrams
DrawingVisual visual = new DrawingVisual();
visual.DrawDrawing(group);
...will correctly render the drawing graph, but the resulting visual does not contain any child visual; only a single visual with no child is created.
The children of the root may also have children with transform(s), which may be the cause of my current failed attempt.
I need the nested visual for performing interactivity operations. Has anyone done a similar thing and is willing to share his/her algorithm?
I have done nearly the same:
All my children are derived from drawingvisual. This is very simplified.
public class VisualParent : DrawingVisual
{
public List<VisualObject> mChildren = new List<VisualObject>();
public VisualCollection mVisuals;
/// <summary>
/// Property AddVisual : Add visual child
/// </summary>
public void AddChild(VisualObject visual)
{
this.mChildren.Add(visual);
}
//Constructor
public VisualParent(Canvas canvas)
{
this.Canvas = canvas;
this.VisualParent = null;
mVisuals = new VisualCollection(canvas);
}
public override void Draw(DrawingContext canvas)
{
if (canvas == null)
{
throw new ArgumentNullException("drawingContext");
}
canvas.DrawRectangle(new SolidColorBrush(BackgroundColor), new Pen(new SolidColorBrush(ForegroundColor), ActualLineWidth), Rectangle);
base.Draw(canvas);
if (IsSelected)
{
DrawHandles(canvas);
}
// Draw children
foreach (VisualObject obj in this.mChildren)
{
obj.Draw(canvas);
}
}
}
Since drawingVisual doesn't manage the children, you must draw all the children yourself.
If you want to move or resize the objects, you have to do the same operation to the objects.
精彩评论