Working with Automated Annotations in WinRT (Windows Store)

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.

  1. Start Visual Studio .NET 2012.
  2. Choose File->New->Project... from the menu.
  3. In the New Project dialog box, choose Visual C# and Windows Store as the project template, and choose Blank App (XAML) in the Templates List.
  4. Type the project name as WinRT Automation in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.
  5. 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.

    Browse to the <LEADTOOLS_INSTALLDIR>\Bin\WinRT8_1\ folder (depending on your target platform), and 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

    Click the Select button and then click the OK button to add the above references to the application.

  6. Open the MainPage.xaml file and copy the below XAML code into the editor:

    [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> 

  7. In the "Solution Explorer" window, right-click "Assets" and select "Add" from the context menu. Then select "Existing Item..." and browse to the file you want displayed in the Image viewer. After adding your file, open the MainPage.xaml file and update the following: Source="Assets/YourFile.jpg" by replacing "YourFile.jpg" with the name of the file you just added.

  8. Switch to 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:

    C#
    using Windows.Storage; 
    using Windows.Storage.Pickers; 
    using Leadtools; 
    using Leadtools.Annotations.Automation; 
    using Leadtools.Annotations.Core; 
                                 
         

  9. Switch to MainPage.xaml code view (right-click MainPage.xaml in the solution explorer then select View Code) and add the following class level variable:

    C#
    AnnAutomation _automation; 
                                                         
         

  10. Update the MainPage() function as shown below:

    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; 
    } 
                                                                 
         

  11. Add the following class functions:

    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); 
       } 
    } 
                     
                                                 
         

  12. 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 Class and name it AutomationControl from Code tree view item in Visual C#

    Click the Add button to add the above file to the application.

  13. Open AutomationControl.cs code view (right-click AutomationControl.cs in the solution explorer then select View Code) and add the following lines at the beginning of the file:

    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; 
                                                         
         

  14. 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

    C#
    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 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; } 
    } 
                      
    public RasterImage Image 
    { 
       get { return _viewer.Image; } 
    } 
                      
                                 
         

  15. 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.

Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document
Click or drag to resize