2014-05-17 16 views
5

Ho difficoltà a trovare il modo migliore per disegnare la forma sottostante. Sto usando il codice qui sotto per disegnare un Ellipse sul livello visivo.Pennello solo parti di un Ellipse in WPF

Ma come posso solo spazzolare i quarti? Penso che si possa fare usando LinearGradientBrush o RadialGradientBrush ma non so come usarlo.

var cntrpoint = space.FlipYAxis(x2, y2); 
dc.DrawEllipse(Brushes.Transparent, pen, cntrpoint, 30, 30); 

enter image description here

risposta

3

Ti piace questa:

var geometry = new GeometryGroup(); 
geometry.Children.Add(new RectangleGeometry(new Rect(1, 0, 1, 1))); 
geometry.Children.Add(new RectangleGeometry(new Rect(0, 1, 1, 1))); 
var drawing = new GeometryDrawing(Brushes.Black, null, geometry); 
var brush = new DrawingBrush(drawing); 

dc.DrawEllipse(brush, pen, cntrpoint, 30, 30); 
+0

Grazie mille Clemens. Per favore, dimmi cosa sta facendo 'RectangleGeometry' qui? Ho difficoltà a capire quella parte. – Vahid

+1

I due RectangleGeometries (di larghezza e altezza '1') riempiono i quadranti inferiore sinistro e superiore destro di una griglia 2x2. Da qui le coordinate (relative) '(1, 0)' e '(0, 1)'. – Clemens

+0

Questa risposta è davvero elegante. Grazie. – Vahid

1

ho trovato una soluzione per farlo in XAML
(ispirata a questo post: https://stackoverflow.com/a/5670388/3047078):

<Image Width="200" Height="200" Margin="20"> 
    <Image.Source> 
    <DrawingImage> 
     <DrawingImage.Drawing> 
     <DrawingGroup> 

      <GeometryDrawing Brush="Black"> 
      <GeometryDrawing.Pen> 
       <Pen Brush="Black" /> 
      </GeometryDrawing.Pen> 
      <GeometryDrawing.Geometry> 
       <PathGeometry> 
       <PathFigure StartPoint="100,100"> 
        <PathFigure.Segments> 
        <LineSegment Point="100,0"/> 
        <ArcSegment Point="200,100" SweepDirection="Clockwise" Size="100,100"/> 
        <LineSegment Point="100,100"/> 
        </PathFigure.Segments> 
       </PathFigure> 
       </PathGeometry> 
      </GeometryDrawing.Geometry> 
      </GeometryDrawing> 

      <GeometryDrawing Brush="White"> 
      <GeometryDrawing.Pen> 
       <Pen Brush="Black"/> 
      </GeometryDrawing.Pen> 
      <GeometryDrawing.Geometry> 
       <PathGeometry> 
       <PathFigure StartPoint="200,100"> 
        <PathFigure.Segments> 
        <ArcSegment Point="100,200" SweepDirection="Clockwise" Size="100,100"/> 
        <LineSegment Point="100,100"/> 
        </PathFigure.Segments> 
       </PathFigure> 
       </PathGeometry> 
      </GeometryDrawing.Geometry> 
      </GeometryDrawing> 

      <GeometryDrawing Brush="Black"> 
      <GeometryDrawing.Pen> 
       <Pen Brush="Black"/> 
      </GeometryDrawing.Pen> 
      <GeometryDrawing.Geometry> 
       <PathGeometry> 
       <PathFigure StartPoint="100,100"> 
        <PathFigure.Segments> 
        <LineSegment Point="100,200"/> 
        <ArcSegment Point="0,100" SweepDirection="Clockwise" Size="100,100"/> 
        <LineSegment Point="100,100"/> 
        </PathFigure.Segments> 
       </PathFigure> 
       </PathGeometry> 
      </GeometryDrawing.Geometry> 
      </GeometryDrawing> 

      <GeometryDrawing Brush="White"> 
      <GeometryDrawing.Pen> 
       <Pen Brush="Black"/> 
      </GeometryDrawing.Pen> 
      <GeometryDrawing.Geometry> 
       <PathGeometry> 
       <PathFigure StartPoint="100,100"> 
        <PathFigure.Segments> 
        <LineSegment Point="0,100"/> 
        <ArcSegment Point="100,0" SweepDirection="Clockwise" Size="100,100"/> 
        </PathFigure.Segments> 
       </PathFigure> 
       </PathGeometry> 
      </GeometryDrawing.Geometry> 
      </GeometryDrawing> 

     </DrawingGroup> 
     </DrawingImage.Drawing> 
    </DrawingImage> 
    </Image.Source> 
</Image> 
+0

Grazie piatto, fa il lavoro, ma quello che voglio è una soluzione che implementa un 'Brush'. Non voglio farlo combinando geometrie separate. – Vahid

1

È possibile eseguire questa operazione utilizzando DrawingBrush e GeometryDrawing

<Ellipse Width="300" Height="300" Stroke="DarkGray" StrokeThickness="1.5"> 
    <Ellipse.Fill> 
     <DrawingBrush> 
      <DrawingBrush.Drawing> 
       <GeometryDrawing> 
        <GeometryDrawing.Brush> 
         <RadialGradientBrush> 
          <GradientStop Color="Black" Offset="1.0" /> 
          <GradientStop Color="White" Offset="1.0" /> 
         </RadialGradientBrush> 
        </GeometryDrawing.Brush> 
        <GeometryDrawing.Geometry> 
         <GeometryGroup> 
          <RectangleGeometry Rect="0,0,50,50" /> 
          <RectangleGeometry Rect="50,50,50,50" /> 
         </GeometryGroup> 
        </GeometryDrawing.Geometry> 
       </GeometryDrawing> 
      </DrawingBrush.Drawing> 
     </DrawingBrush> 
    </Ellipse.Fill> 
</Ellipse> 

uscita:

enter image description here

+0

Grazie, non so come adattarlo alla mia situazione. Perché sto disegnando l'ellisse nel codice dietro. – Vahid