StrokeCollection.GetBounds Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Returns the bounds of the strokes in the collection.

Namespace: System.Windows.Ink
Assembly: System.Windows (in System.Windows.dll)

Syntax

Public Function GetBounds As Rect
public Rect GetBounds()

Return Value

Type: System.Windows..::.Rect
A Rect that contains the bounds of the strokes in the StrokeCollection.

Remarks

Do not attempt to set values in the returned Rect. It should be used for reference only.

There are various scenarios for using the returned Rect. The Rect can be used as input for a RectangleGeometry that displays a bounding box in the user interface, or it can be used to check for intersecting strokes or other types of hit-testing.

Examples

The following code example demonstrates the GetBounds method of the StrokeCollection object. After a stroke is drawn, a bounding box that surrounds the stroke is displayed.

    Private MyStroke As Stroke

    Private MyRectangle As Rectangle = Nothing

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        SetBoundary()
    End Sub

    'A new stroke object, MyStroke, is created and is added to the StrokeCollection object
    'of the InkPresenter, MyIP
    Private Sub MyIP_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseEventArgs)
        MyIP.CaptureMouse()
        Dim MyStylusPointCollection As StylusPointCollection = New StylusPointCollection
        MyStylusPointCollection.Add(e.StylusDevice.GetStylusPoints(MyIP))
        MyStroke = New Stroke(MyStylusPointCollection)
        MyIP.Strokes.Add(MyStroke)
    End Sub

    'StylusPoint objects are collected from the MouseEventArgs and added to MyStroke
    Private Sub MyIP_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
        If (Not (MyStroke) Is Nothing) Then
            MyStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(MyIP))
        End If
    End Sub

    'GetBounds() method of the StrokeCollection is used to draw a rectangular boundary around the StrokeCollection
    Private Sub MyIP_LostMouseCapture(ByVal sender As Object, ByVal e As MouseEventArgs)
        Dim MyRect As Rect = New Rect
        MyRect = MyIP.Strokes.GetBounds
        If (MyRectangle Is Nothing) Then
            MyRectangle = New Rectangle
            MyIP.Children.Add(MyRectangle)
        End If
        MyRectangle.Height = MyRect.Height
        MyRectangle.Width = MyRect.Width
        Dim MyThickness As Thickness = New Thickness(MyRect.X, MyRect.Top, 0, 0)
        MyRectangle.Margin = MyThickness
        Dim MyBrush As SolidColorBrush = New SolidColorBrush(Colors.Black)
        MyRectangle.Stroke = MyBrush
        MyStroke = Nothing
    End Sub
Stroke MyStroke;
Rectangle MyRectangle = null;

//A new stroke object named MyStroke is created. MyStroke is added to the StrokeCollection of the InkPresenter named MyIP
private void MyIP_MouseLeftButtonDown(object sender, MouseEventArgs e)
{
    MyIP.CaptureMouse();
    StylusPointCollection MyStylusPointCollection = new StylusPointCollection();
    MyStylusPointCollection.Add(e.StylusDevice.GetStylusPoints(MyIP));
    MyStroke = new Stroke(MyStylusPointCollection);

    MyIP.Strokes.Add(MyStroke);
}

//StylusPoint objects are collected from the MouseEventArgs and added to MyStroke
private void MyIP_MouseMove(object sender, MouseEventArgs e)
{
    if (MyStroke != null)
        MyStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(MyIP));
}

//GetBounds() method of the StrokeCollection is used to draw a rectangular boundary around the StrokeCollection
private void MyIP_LostMouseCapture(object sender, MouseEventArgs e)
{
    Rect MyRect = new Rect();
    MyRect = MyIP.Strokes.GetBounds();
    if (MyRectangle == null)
    {
        MyRectangle = new Rectangle();
        MyIP.Children.Add(MyRectangle);
    }
    MyRectangle.Height = MyRect.Height;
    MyRectangle.Width = MyRect.Width;
    Thickness MyThickness = new Thickness(MyRect.X, MyRect.Top, 0, 0);
    MyRectangle.Margin = MyThickness;
    SolidColorBrush MyBrush = new SolidColorBrush(Colors.Black);
    MyRectangle.Stroke = MyBrush;

    MyStroke = null;
}

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

StrokeCollection Class

System.Windows.Ink Namespace

Stroke

GetBounds