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

我有一个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);
    }
}
点赞