Ellipse in WPF


The Ellipse object represents an ellipse shape and draws an ellipse with the given height and width. The Width and Height properties of the Ellipse class represent the width and height of an ellipse. The Fill property fills the interior of an ellipse. The Stroke property sets the color and StrokeThickness represents the width of the outer line of an ellipse.

Creating an Ellipse

The Ellipse element in XAML creates an ellipse shape. The following code snippet creates an ellipse by setting its width and height properties to 200 and 100 respectively. The code also sets the black stroke of width 4. 

<Ellipse

    Width="200"

    Height="100"

    Fill="Blue"

    Stroke="Black"

    StrokeThickness="4" />

 

The output looks like Figure 7.

EllipseImg1.gif 

Figure 7. An Ellipse

The CreateAnEllipse method listed in Listing 8 draws same rectangle in Figure 7 dynamically.

/// <summary>

/// Creates a blue ellipse with black border

/// </summary>

public void CreateAnEllipse()

{

    // Create an Ellipse

    Ellipse blueRectangle = new Ellipse();

    blueRectangle.Height = 100;

    blueRectangle.Width = 200;

 

    // Create a blue and a black Brush

    SolidColorBrush blueBrush = new SolidColorBrush();

    blueBrush.Color = Colors.Blue;

    SolidColorBrush blackBrush = new SolidColorBrush();

    blackBrush.Color = Colors.Black;

 

    // Set Ellipse's width and color

    blueRectangle.StrokeThickness = 4;

    blueRectangle.Stroke = blackBrush;

    // Fill rectangle with blue color

    blueRectangle.Fill = blueBrush;

 

    // Add Ellipse to the Grid.

    LayoutRoot.Children.Add(blueRectangle);

}

Listing 7


A circle is an ellipse with an equal width and height. If you set both width and height to 200 in the above code listed in Listing 7, it will generate a circle.

erver'>
Up Next
    Ebook Download
    View all
    Learn
    View all