Stroke.GetBounds Method

July 28, 2014

Retrieves the bounding box for the Stroke object.

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

public Rect GetBounds()

Return Value

Type: System.Windows.Rect
A Rect structure defining the bounding box for the Stroke object.

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.

The following code example demonstrates the GetBounds method of the Stroke object.


Stroke MyStroke;

//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 is used to draw a rectangular boundary on every stroke 
//as soon as the stroke is completed.
private void MyIP_LostMouseCapture(object sender, MouseEventArgs e)
{
    Rect MyRect = new Rect();
    MyRect = MyStroke.GetBounds();
    Rectangle MyRectangle = new Rectangle();
    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;
    MyIP.Children.Add(MyRectangle);
    MyStroke = null;
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft