0

In my wpf application i am drawing a lot of geometries as below. My requirement is to change the color of drawingvisual with out redrawing it? any possibilities in wpf?

  using (DrawingContext vDrawingContext = vDrawingVisual.RenderOpen())
        {
          StreamGeometry vGeom = GetCutGeometry(mLength, mWidth);
          vDrawingContext.DrawGeometry(mBackGroundBrush, ForeGroundPen, vGeom);
          vDrawingContext.Close();
          VisualChildren.Add(vDrawingVisual);    

        }

How could be mBackGroundBrush dyamic colors?

2 Answers2

3

Provided that mBackGroundBrush is a modifiable SolidColorBrush (i.e. it is created in your application and none of the predefined brushes), you could simply change its Color property. That will change the fill color of each drawn geometry with redrawing.

private SolidColorBrush mBackGroundBrush = new SolidColorBrush(Colors.Black);

...

mBackGroundBrush.Color = Colors.Red;

or

mBackGroundBrush.Color = Color.FromArgb(255, 255, 0, 0);
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Well, it works perfectly for me. Maybe you post a little bit more code like the declaration of `mBackGroundBrush` and where you change it. – Clemens Jun 05 '13 at 11:26
  • Brush mBackGroundBrush = null; – PropertyChangedEventHandler Jun 05 '13 at 11:33
  • 1
    A Big thanks, I was changing the Brush instance as mBackGroundBrush = new SolidColorBrush(..) that was my mistake.... – PropertyChangedEventHandler Jun 05 '13 at 12:01
  • Clemens, how do you completely "erase" an object that has already been drawn? The `DrawingContext` class only has "draw" methods, nothing else (except a Dispose() method that wipes everything out). – Sabuncu Jun 10 '14 at 19:06
  • @Sabuncu The Dispose method just closes the DrawingContext, but does not "wipe out" anything. In order to remove a certain object from a DrawingVisual, you will usually have to do a complete redraw. – Clemens Jun 10 '14 at 19:09
  • How do I do a "complete" redraw? Do you mean, for example, deleting the GlyphRun object I have created, then building it again and calling DrawGlyphRun()? What I am missing is this: there's some drawing in the DC. How can I get rid of some portion of it - for example, get rid of only the last GlyphRun I have drawn? PS: Should I post a question for this so that you can get credit? Will be happy to. – Sabuncu Jun 10 '14 at 19:15
  • You should indeed post a question. How else should anybody know what there is in your DrawGlyphRun method? But in short, when you call RenderOpen on a DrawingVisual, you already start a complete redraw. There will be nothing "left over" from any previous drawing. – Clemens Jun 10 '14 at 19:19
  • Thanks. My code currently does not use RenderOpen, so I will look into it. I only have OnRender in which I call DrawGlyphRun. – Sabuncu Jun 10 '14 at 19:22
1

I have done one work around as below. seems working.

///Kept as arefrence while initial drawing phase.
private DrawingVisual mDrawingVisual = null;

 if (null != mDrawingVisual)
      {
        using (DrawingContext vDrawingContext = mDrawingVisual.RenderOpen())
        {
          DrawingGroup vDrawingGroup = VisualTreeHelper.GetDrawing(mDrawingVisual);
          if (null != vDrawingGroup)
          {
            foreach (Drawing vDrawing in vDrawingGroup.Children)
            {
              GeometryDrawing vGeometryDrawing = vDrawing as GeometryDrawing;
              if (null != vGeometryDrawing)
              {
                vGeometryDrawing.Brush = mBackGroundBrush;
              }
            }
          }

          vDrawingContext.DrawDrawing(vDrawingGroup);
          vDrawingContext.Close();
        }
      }