Take the following steps to start a project and to add some code that will demonstrate the automated annotation features of LEADTOOLS WinRT Annotations.
In the "Solution Explorer" window, right-click the "References" folder for the project and select "Add Reference..." from the context menu. In the "Reference Manager" dialog box, select Browse and navigate to the "<LEADTOOLS_INSTALLDIR>\Bin\WinRT8_1\" folder (depending on your target platform). Select the following .WINMD files:
Leadtools.winmd
Leadtools.Kernel.winmd
Leadtools.Annotations.Automation.dll
Leadtools.Annotations.Core.dll
Leadtools.Annotations.Designers.dll
Leadtools.Annotations.Rendering.dll
Leadtools.Controls.dll
Click Select and thenOK to add the above references to the application.
Open the MainPage.xaml file and copy the following XAML code into the editor:
[XAML]
<Page
x:Class="WinRT_Automation.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinRT_Automation"
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">Load Annotations</Button>
<Button Name="_saveAnn" Click="SaveAnn_Click">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>
Switch to the MainPage.xaml code view (right-click MainPage.xaml in the solution explorer and then select View Code) and add the following lines at the beginning of the file:
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Leadtools;
using Leadtools.Annotations.Automation;
using Leadtools.Annotations.Core;
Switch to the MainPage.xaml code view (right-click MainPage.xaml in the solution explorer and then select View Code) and add the following class level variable:
AnnAutomation _automation;
Update the MainPage() function as shown below:
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;
}
Add the following class functions:
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();
await codecs.SaveAsync(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.LoadAsync(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 the "Project Name" and select "Add" from the context menu. Then select "New Item...". In the "Add New Item" dialog box, select Class and name it AutomationControl from Code tree view item in Visual C#
Click Add to add the above file to the application.
Open the AutomationControl.cs code view (right-click AutomationControl.cs in the solution explorer and then select View Code) and add the following lines at the beginning of the file:
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;
in
Open AutomationControl.cs code view (right-click AutomationControl.cs in the solution explorer then select View Code) and replace class AutomationControl with public class AutomationControl : IAnnAutomationControl and add the following functionalities to the class.
AnnContainer _container = null;
AnnWinRTRenderingEngine _engine = null;
Canvas _viewer;
LeadPointD _offset = LeadPointDHelper.Create(0, 0);
public AutomationControl(Canvas viewer)
{
if (viewer == null)
{
throw new Exception("Viewer cannot 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 AnnWinRTRenderingEngine(_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
{
set { _engine = value as AnnWinRTRenderingEngine; }
get { return _engine; }
}
public RasterImage Image
{
get { return null;}
}
Build, and Run the program to test it. Use the combo box below the viewer to select annotations. The radio buttons allow you to switch between design and run mode.