WPF: Build rectangle annotation with specified size when user clicked on image.

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

Moderator: Alex

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

WPF: Build rectangle annotation with specified size when user clicked on image.

Post by Alex »

Here is C# example for WPF that shows how to add a rectangle annotation with size 100x100 on image when user clicked on image.

Code: Select all

/// <summary>
/// User started the annotation building.
/// </summary>
private void buildAnnotationButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        // create annotation view
        WpfAnnotationView annotationView = new WpfRectangleAnnotationView(new RectangleAnnotationData());
        if (annotationView != null)
        {
            // subscribe from the Interaction event of annotation builder
            annotationView.Builder.Interaction += Builder_Interaction;

            // start annotation building
            Viewer.AnnotationVisualTool.AddAndBuildAnnotation(annotationView);
        }
    }
    catch (InvalidOperationException ex)
    {
        MessageBox.Show(ex.Message, "Building annotation", MessageBoxButton.OK, MessageBoxImage.Error);
    }
}

/// <summary>
/// Handles the Interaction event of the annotation builder.
/// </summary>
void Builder_Interaction(
    object sender,
    Vintasoft.Imaging.Wpf.UI.VisualTools.UserInteraction.WpfInteractionEventArgs e)
{
    // desired annotation width and height
    double annoWidth = 100;
    double annoHeight = 100;

    // change the start and end location of interaction - this is necessary for correct Location and Size of annotation
    e.StartLocation = e.Location;
    e.Location = new System.Windows.Point(e.Location.X + annoWidth, e.Location.Y + annoHeight);
    // specify that interaction is finished
    e.InteractionFinished = true;

    WpfAnnotationViewer annotationViewer = (WpfAnnotationViewer)e.Viewer;
    WpfAnnotationView annotationView = annotationViewer.FocusedAnnotationView;

    // unsubscribe from the Interaction event of annotation builder
    annotationView.Builder.Interaction -= Builder_Interaction;

    // set annotation parameters (Location and Size will be set by builder)
    annotationView.CanResize = false;
}
Post Reply