views:

110

answers:

2

Hello,

I have a VisualBrush and need this VisualBrush as a Drawing. Anyone knows how this can be done? Thanks for any hint!

+2  A: 

Your question doesn't really make sense, because a VisualBrush is unrelated to a Drawing (it would make more sense with a DrawingBrush). However, you can create a Drawing by using the VisualBrush to paint on it. Something like that should work :

public static Drawing GetDrawing(TileBrush brush)
{
    DrawingVisual drawingVisual = new DrawingVisual();
    DrawingContext drawingContext = drawingVisual.RenderOpen();
    drawingContext.DrawRectangle(brush, new Pen(Brushes.Transparent, 0.0), brush.ViewPort);
    drawingContext.Close();
    return drawingVisual.Drawing;
}

(this is valid for any brush inherited from TileBrush, not just a VisualBrush)

Thomas Levesque
Thanks for your reply, looks very promising :-)Note that you can just set the pen to nulldrawingContext.DrawRectangle(brush, null, 0.0), brush.ViewPort);
stefan.at.wpf
+1  A: 

Here's the XAML version:

<GeometryDrawing Geometry="M0,0 L1,0 1,1 0,1 Z">
  <GeometryDrawing.Brush>
    <VisualBrush>
      ...
    </VisualBrush>
  </GeometryDrawing.Brush>
</GeometryDrawing>
Ray Burns