Take the following steps to start a project and to add some code that will demonstrate the automated annotation features of the LEADTOOLS WinRT Annotations.
In the "Solution Explorer" window, right-click on the "References" folder for the project and select "Add Reference…" from the context menu. In the "Reference Manager" dialog box, select
Browse to the "<LEADTOOLS_INSTALLDIR>\Bin\WinRT\" folder (depending on your target platform), and select the following .WINMD files:
[XAML]
<Page x:Class="App1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="4*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Image HorizontalAlignment="Stretch" Height="800" Source="Assets/YourFile.jpg"></Image> <Canvas HorizontalAlignment="Stretch" Height="800" Background="Transparent" Name="_viewer"/> <StackPanel Orientation="Vertical" x:Name="stackPanel" Grid.Row="1"> <ComboBox Name="_annObjects" SelectionChanged="annObjects_SelectionChanged"/> <Button Name="_loadAnn" Click="_loadAnn_Click_1">Load Annotations</Button> <Button Name="_saveAnn" Click="_saveAnn_Click_1">Save Annotations</Button> <RadioButton x:Name="_rbRunMode" Content="Run Mode" Checked="AnnotationMode_Changed" HorizontalAlignment="Center" FontSize="20"/> <RadioButton x:Name="_rbDesignMode" Content="Design Mode" Checked="AnnotationMode_Changed" HorizontalAlignment="Center" FontSize="20"/> </StackPanel> </Grid> </Page>
[C#]
using Windows.Storage; using Windows.Storage.Pickers; using Leadtools; using Leadtools.Annotations.Automation; using Leadtools.Annotations.Core;
[C#]
AnnAutomation _automation;
[C#]
public MainPage() { RasterSupport.Initialize(); AnnAutomationManager manager = new AnnAutomationManager(); manager.CreateDefaultObjects(); AutomationControl viewerControl = new AutomationControl(_viewer); _automation = new AnnAutomation(manager, viewerControl); _rbDesignMode.IsChecked = true; _automation.Active = true; AddItem(_annObjects, AnnObject.SelectObjectId); AddItem(_annObjects, AnnObject.PointerObjectId); AddItem(_annObjects, AnnObject.RectangleObjectId); AddItem(_annObjects, AnnObject.TextObjectId); AddItem(_annObjects, AnnObject.RulerObjectId); _annObjects.SelectedIndex = 0; }
[C#]
private void AnnotationMode_Changed(object sender, RoutedEventArgs e) { _automation.Manager.UserMode = (bool)_rbDesignMode.IsChecked ? AnnUserMode.Design : AnnUserMode.Run; } private void annObjects_SelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBoxItem item = _annObjects.SelectedItem as ComboBoxItem; _automation.Manager.CurrentObjectId = (int)item.Tag; } private void AddItem(ComboBox combo, int id) { ComboBoxItem item = new ComboBoxItem(); item.Tag = id; item.Content = _automation.Manager.FindObjectById(id).ObjectTemplate.FriendlyName; combo.Items.Add(item); } private async void SaveAnn_Click(object sender, RoutedEventArgs e) { FileSavePicker filePicker = new FileSavePicker(); List<string> filters = new List<string>(); filters.Add(".xml"); filePicker.FileTypeChoices.Add("Xml file", filters); StorageFile file = await filePicker.PickSaveFileAsync(); if (file != null) { AnnCodecs codecs = new AnnCodecs(); codecs.Save(file, _automation.Container, Leadtools.Annotations.Core.AnnFormat.Annotations, 0); } } private async void LoadAnn_Click(object sender, RoutedEventArgs e) { AnnCodecs codecs = new AnnCodecs(); FileOpenPicker filePicker = new FileOpenPicker(); filePicker.FileTypeFilter.Add(".xml"); StorageFile file = await filePicker.PickSingleFileAsync(); if (file != null) { IRandomAccessStream stream = await file.OpenReadAsync(); AnnContainer container = await codecs.Load(file, 1); _automation.Container.Children.Clear(); foreach (AnnObject annObject in container.Children) { _automation.Container.Children.Add(annObject); } _automation.Invalidate(LeadRectDHelper.Empty); } }
In the "Solution Explorer" window, right-click on the "Project Name" and select "Add" from the context menu then select "New Item...". In the "Add New Item" dialog box, select
Classand Name it AutomationControlstrong> from Code tree view item in Visual C#
[C#]
using Leadtools.Annotations.Core; using Leadtools.Annotations.Rendering; using Windows.UI.Xaml.Controls; using Leadtools; using Windows.UI.Xaml.Input; using Windows.Foundation; using Windows.UI.Xaml.Media;
[C#]
AnnContainer _container = null; AnnMetroRenderingEngine _engine = null; Canvas _viewer; LeadPointD _offset = LeadPointDHelper.Create(0, 0); public AutomationControl(Canvas viewer) { if (viewer == null) { throw new Exception("Viewer can not be null"); } _viewer = viewer; _viewer.PointerPressed += _viewer_PointerPressed; _viewer.PointerMoved += _viewer_PointerMoved; _viewer.PointerReleased += _viewer_PointerReleased; } void _viewer_PointerReleased(object sender, PointerRoutedEventArgs e) { if (AutomationPointerUp != null) { Point position = e.GetCurrentPoint(_viewer).Position; AutomationPointerUp(this, AnnPointerEventArgs.Create(AnnMouseButton.Left, AutomationContainer.Mapper.PointToContainerCoordinates(LeadPointDHelper.Create(position.X, position.Y)))); if (_viewer != null) { this.AutomationInvalidate(LeadRectDHelper.Empty); } } } void _viewer_PointerMoved(object sender, PointerRoutedEventArgs e) { if (AutomationPointerMove != null) { Point position = e.GetCurrentPoint(_viewer).Position; AutomationPointerMove(this, AnnPointerEventArgs.Create(AnnMouseButton.Left, AutomationContainer.Mapper.PointToContainerCoordinates(LeadPointDHelper.Create(position.X, position.Y)))); this.AutomationInvalidate(LeadRectDHelper.Empty); } } void _viewer_PointerPressed(object sender, PointerRoutedEventArgs e) { if (AutomationPointerDown != null) { Point position = e.GetCurrentPoint(_viewer).Position; AutomationPointerDown(this, AnnPointerEventArgs.Create(AnnMouseButton.Left, AutomationContainer.Mapper.PointToContainerCoordinates(LeadPointDHelper.Create(position.X, position.Y)))); this.AutomationInvalidate(LeadRectDHelper.Empty); } } public AnnContainer AutomationContainer { get { return _container; } } public double AutomationDpiX { get { return 96.0; } } public double AutomationDpiY { get { return 96.0; } } public bool AutomationEnabled { get { return true; } } public LeadSizeD AutomationSize { get { return LeadSizeDHelper.Create(_viewer.ActualWidth, _viewer.ActualHeight); } } public event EventHandler AutomationSizeChanged; public LeadMatrix AutomationTransform { get { MatrixTransform transform = _viewer.RenderTransform as MatrixTransform; Matrix matrix = transform.Matrix; return new LeadMatrix(matrix.M11, matrix.M12, matrix.M21, matrix.M22, matrix.OffsetX, matrix.OffsetY); } } public event EventHandler AutomationTransformChanged; public bool AutomationUseDpi { get { return false; } } public event EventHandler AutomationUseDpiChanged; public double AutomationXResolution { get { return 96.0; } } public double AutomationYResolution { get { return 96.0; } } public event EventHandler AutomationEnabledChanged; public event EventHandler AutomationGotFocus; public event EventHandler AutomationLostFocus; public event EventHandler<AnnPointerEventArgs> AutomationDoubleClick; public event EventHandler<AnnPointerEventArgs> AutomationPointerDown; public event EventHandler<AnnPointerEventArgs> AutomationPointerMove; public event EventHandler<AnnPointerEventArgs> AutomationPointerUp; public void AutomationAttach(AnnContainer container) { _container = container; _engine = new AnnMetroRenderingEngine(_container, _viewer); if (_engine != null) { _engine.Render(LeadRectDHelper.Create(0, 0, _viewer.Width, _viewer.Height), true); } } public void AutomationDetach() { _container = null; } public void AutomationInvalidate(LeadRectD rc) { if (_engine != null) { _engine.Render(LeadRectDHelper.Create(0, 0, _viewer.ActualWidth, _viewer.ActualHeight), true); } } public LeadPointD AutomationOffset { get { return _offset; } } public AnnRenderingEngine RenderingEngine { get { return _engine; } }
Build, and Run the program to test it.
Use the combo box below the viewer to select annotations. The radio buttons will allow you to switch between design and run mode.