4

I have a DrawingVisual and want to draw a grape tree, show to screen then after that, draw a fox. 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. So I want to ask how to keep existing drawing content when draw new geometry? Thank!

NoName
  • 7,940
  • 13
  • 56
  • 108

1 Answers1

2

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. 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);
    }
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • Thank for the answer, that is pretty clear! All I need is display geometries one by one to screen, otherwise do nothing with them. I have thoundsands geometries to display, so add them as as shapes/paths element is not make sense. Now one solution I know is render the `Visual` to a `RenderTargetBitmap` then add that bitmap to a Image control. But render it take quite a lot time and cpu. Do you know any way better than that? – NoName Oct 31 '15 at 10:25
  • @TuyenTk: _"Do you know any way better than that"_ -- sorry...lacking more details, that'd be too broad to answer even as an actual Stack Overflow question, never mind as a comment. It depends on too many different things. Do note that in your current approach, the visual still needs to store a graph of your "thousands of geometries", except as `Drawing` objects (so they are actually more data than just `Geometry` objects). A custom control might give you better performance (if indeed you are running into performance problems). – Peter Duniho Oct 31 '15 at 17:10
  • @TuyenTk: as for the option of rendering to a bitmap, that should be the _least_ impactful performance-wise, if done correctly, because you should only need to draw to the bitmap as new objects are added; once they are drawn into the bitmap, actually displaying the bitmap is very efficient. The biggest drawback there is that the bitmap has a fixed number of pixels/resolution, and so can't accommodate changes in window/container size. But that's often not a drawback in practice, i.e. when the bitmap is only ever going to be displayed at a single size. – Peter Duniho Oct 31 '15 at 17:13