gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:24:39 25 4
gpt4 key购买 nike

我有一个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/

25 4 0
文章推荐: c# - 绑定(bind)到 IEnumerable 依赖属性