Search code examples
c#wpfdrawingbrushpen

WPF Drawing context


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?


Solution

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