WPF: Create rectangle annotation programmatically.

Code samples for VintaSoft Annotation .NET Plug-in. Here you can request a code sample.

Moderator: Alex

Post Reply
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

WPF: Create rectangle annotation programmatically.

Post by Alex »

In most cases rectangle on image is specified using the following parameters:
  • Coordinates of left-top corner of rectangle
  • Size of rectangle
  • Rotation angle of rectangle
Rectangle annotation on image is specified using the following parameters:
  • Coordinates of center of rectangle annotation
  • Size of rectangle annotation
  • Rotation angle of rectangle annotation

Here is C# example for WPF that shows how to create not rotated rectangle annotation if rectangle on image is specified using coordinates of left-top corner of rectangle and rectangle size:

Code: Select all

/// <summary>
/// Creates not rotated rectangle annotation.
/// </summary>
/// <param name="rectLeftTop">The left top coordinates of rectangle.</param>
/// <param name="rectSize">Size of rectangle.</param>
/// <returns>Not rotated rectangle annotation.</returns>
private WpfRectangleAnnotationView CreateNotRotatedRectangleAnnotationView(
    System.Windows.Point rectLeftTop,
    System.Windows.Size rectSize)
{
    // center of not rotated rectangle 
    System.Windows.Point rectCenter = new System.Windows.Point(rectLeftTop.X + rectSize.Width / 2, rectLeftTop.Y + rectSize.Height / 2);

    // create rectangle annotation
    RectangleAnnotationData rectAnnoData = new RectangleAnnotationData();
    WpfRectangleAnnotationView rectAnnoView = new WpfRectangleAnnotationView(rectAnnoData);
    // specify annotation location as center of rotated rectangle
    rectAnnoView.Location = rectCenter;
    // specify annotation size
    rectAnnoView.Size = rectSize;

    // return the rectangle annotation
    return rectAnnoView;
}

Here is C# example for WPF that shows how to create rotated rectangle annotation if rectangle on image is specified using coordinates of left-top corner of rectangle, rectangle size and rotation angle of rectangle:

Code: Select all

/// <summary>
/// Creates rotated rectangle annotation.
/// </summary>
/// <param name="rectLeftTop">The left top coordinates of rectangle.</param>
/// <param name="rectSize">Size of rectangle.</param>
/// <param name="rectRotation">Rotation angle, in degrees, of rectangle.</param>
/// <returns>Rotated rectangle annotation.</returns>
private WpfRectangleAnnotationView CreateRotatedRectangleAnnotationView(
    System.Windows.Point rectLeftTop,
    System.Windows.Size rectSize,
    double rectRotation)
{
    // center of not rotated rectangle 
    System.Windows.Point rectCenter = new System.Windows.Point(rectSize.Width / 2, rectSize.Height / 2);

    // create Matrix
    System.Windows.Media.Matrix matrix = new System.Windows.Media.Matrix();
    // rotate Matrix
    matrix.Rotate(rectRotation);
    // translate Matrix
    matrix.Translate(rectLeftTop.X, rectLeftTop.Y);
    // transform center of not rotated rectangle using Matrix AND get center of rotated rectangle
    rectCenter = matrix.Transform(rectCenter);

    // create rectangle annotation
    RectangleAnnotationData rectAnnoData = new RectangleAnnotationData();
    WpfRectangleAnnotationView rectAnnoView = new WpfRectangleAnnotationView(rectAnnoData);
    // specify annotation location as center of rotated rectangle
    rectAnnoView.Location = rectCenter;
    // specify annotation size
    rectAnnoView.Size = rectSize;
    // specify annotation rotation
    rectAnnoView.Rotation = (float)rectRotation;

    // return the rectangle annotation
    return rectAnnoView;
}
Post Reply