WPF: Place a sticky note annotation on the image center in annotation viewer.

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: Place a sticky note annotation on the image center in annotation viewer.

Post by Alex »

This topic contains C# code snippet that shows how to place a sticky note annotation on the image center in a WPF annotation viewer.

Code: Select all

using System;
using System.Windows;

using Microsoft.Win32;

using Vintasoft.Imaging;
using Vintasoft.Imaging.Annotation;
using Vintasoft.Imaging.Utils;


namespace WpfApplication
{
    /// <summary>
    /// Main form.
    /// </summary>
    public partial class Window1 : Window
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="Window1"/> class.
        /// </summary>
        public Window1()
        {
            InitializeComponent();
        }



        /// <summary>
        /// Clears image collection of image viewer and 
        /// adds image(s) to an image collection of image viewer.
        /// </summary>
        private void openButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == true)
            {
                annotationViewer.Images.ClearAndDisposeItems();

                try
                {
                    annotationViewer.Images.Add(dialog.FileName);
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        /// <summary>
        /// Adds the sticky note annotation in the center of annotation viewer.
        /// </summary>
        private void addStickyNoteAnnotation_Click(object sender, RoutedEventArgs e)
        {
            VintasoftImage image = annotationViewer.Image;
            if (image == null)
                return;

            // calculate location of annotation in pixels
            Point annotationLocation = new Point(image.Width / 2.0, image.Height / 2.0);
            // calculate size of annotation in pixels
            Size annotationSize = new Size(image.Width / 4.0, image.Height / 4.0);

            // create the annotation data
            AnnotationData data = CreateStickyNoteAnnotation(annotationLocation, annotationSize, image.Resolution);

            // add the annotation to the viewer
            annotationViewer.AnnotationDataCollection.Add(data);
            // select the annotation in the viewer
            annotationViewer.FocusedAnnotationData = data;
        }

        /// <summary>
        /// Creates the sticky note annotation.
        /// </summary>
        /// <param name="location">The location of annotation in pixels.</param>
        /// <param name="size">The size of annotation in pixels.</param>
        /// <param name="imageResolution">The resolution of image.</param>
        private AnnotationData CreateStickyNoteAnnotation(Point location, Size size, Resolution imageResolution)
        {
            // create the annotation data
            StickyNoteAnnotationData data = new StickyNoteAnnotationData();

            // convert location from pixels to DIP
            double x = UnitOfMeasureConverter.ConvertToDeviceIndependentPixels(
                location.X, UnitOfMeasure.Pixels, imageResolution.Horizontal);
            double y = UnitOfMeasureConverter.ConvertToDeviceIndependentPixels(
                location.Y, UnitOfMeasure.Pixels, imageResolution.Vertical);
            data.Location = new System.Drawing.PointF((float)x, (float)y);

            // convert size from pixels to DIP
            double width = UnitOfMeasureConverter.ConvertToDeviceIndependentPixels(
                size.Width, UnitOfMeasure.Pixels, imageResolution.Horizontal);
            double height = UnitOfMeasureConverter.ConvertToDeviceIndependentPixels(
                size.Height, UnitOfMeasure.Pixels, imageResolution.Vertical);
            data.Size = new System.Drawing.SizeF((float)width, (float)height);

            return data;
        }

        /// <summary>
        /// Raises the Closed event.
        /// </summary>
        protected override void OnClosed(EventArgs e)
        {
            annotationViewer.Images.ClearAndDisposeItems();

            base.OnClosed(e);
        }

    }
}
Post Reply