c# - WPF 绘图上下文 : How to keep existing content when draw new content?

标签 c# wpf geometry 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);
    }
}

关于c# - WPF 绘图上下文 : How to keep existing content when draw new content?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447959/

相关文章:

c# - 接口(interface)作为类型约束和接口(interface)作为参数之间的区别?

c# - mvvm light - 发送带回调的通知消息

c# - 简单的普通 Windows 应用程序——我应该从 WPF 还是 WinForms 开始?

c++ - 绘制顺时针三角形重新排序点

c# - VisualStateManager.GoToState 不适用于 DataTemplate

c# - 使用 goto 的最佳实践

wpf - WPF Dispatcher 是多线程问题的解决方案吗?

mysql - 如何使用带有 MySQL 的 Haversine 公式测量距离?

algorithm - 计算两组点之间的最小距离的最快算法是什么?

c# - 错误 CS0246 : The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?)