WPF DrawingContext:绘制新内容时如何保留现有内容?

WPF DrawingContext: How to keep existing content when draw new content?

我有一个DrawingVisual,想画一棵葡萄树,显示到屏幕上,然后再画一只狐狸。像这样:

public class Gif : DrawingVisual
{
    void Draw_Geometry(Geometry geo)
    {
        using (DrawingContext dc = RenderOpen())
        {
            dc.DrawGeometry(Brushes.Brown, new Pen(Brushes.Brown, 0), geo);
        }
    }

    void Draw_Grape ()
    {
        Draw_Geometry(grape);
    }

    void Draw_Fox ()
    {
        Draw_Geometry(fox);
    }
}

问题是在调用 Draw_Fox () 时,DrawingContext 会自动清除现有的葡萄树。所以我想问一下在绘制新的几何体时如何保留已有的绘制内容?谢谢!

来自文档:

When you call the Close method of the DrawingContext, the current drawing content replaces any previous drawing content defined for the DrawingVisual. This means that there is no way to append new drawing content to existing drawing content.

我觉得说的很清楚了。不可能按照你的要求去做。打开视觉对象进行渲染将总是以新渲染结束,替换之前的任何内容。

如果要附加当前渲染,则需要明确包含它。例如:

void Draw_Geometry(Geometry geo)
{
    using (DrawingContext dc = RenderOpen())
    {
        dc.DrawDrawing(Drawing);
        dc.DrawGeometry(Brushes.Brown, new Pen(Brushes.Brown, 0), geo);
    }
}