简体   繁体   English

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

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

I have a DrawingVisual and want to draw a grape tree, show to screen then after that, draw a fox. 我有一个DrawingVisual ,想要绘制一棵葡萄树,然后显示屏幕然后画出一只狐狸。 Like this: 像这样:

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);
    }
}

Problem is when call Draw_Fox () , the DrawingContext auto clear existing grape tree. 问题是当调用Draw_Fox ()DrawingContext自动清除现有的葡萄树。 So I want to ask how to keep existing drawing content when draw new geometry? 所以我想问一下在绘制新几何体时如何保留现有的绘图内容? Thank! 谢谢!

From the documentation: 从文档:

When you call the Close method of the DrawingContext, the current drawing content replaces any previous drawing content defined for the DrawingVisual. 当您调用DrawingContext的Close方法时,当前绘图内容将替换为DrawingVisual定义的任何先前绘图内容。 This means that there is no way to append new drawing content to existing drawing content. 这意味着无法将新绘图内容附加到现有绘图内容。

I feel like that's pretty clear. 我觉得这很清楚。 It is not possible to do literally what you ask. 不可能按字面意思做你所要求的。 Opening the visual for rendering will always end up with the new rendering replacing whatever was there before. 打开渲染的视觉效果将始终以新渲染取代以前的渲染。

If you want to append the current rendering, you need to include it explicitly. 如果要追加当前渲染,则需要显式包含它。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM