简体   繁体   English

WPF绘图上下文

[英]WPF Drawing context

In my wpf application i am drawing a lot of geometries as below. 在我的wpf应用程序中,我绘制了许多几何图形,如下所示。 My requirement is to change the color of drawingvisual with out redrawing it? 我的要求是在不重绘的情况下更改drawingvisual的颜色吗? any possibilities in wpf? 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? mBackGroundBrush的动态颜色如何?

Provided that mBackGroundBrush is a modifiable SolidColorBrush (ie it is created in your application and none of the predefined brushes), you could simply change its Color property. 如果mBackGroundBrush是可修改的SolidColorBrush (即在您的应用程序中创建,并且没有预定义的画笔),则只需更改其Color属性即可。 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);

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();
        }
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM