LEADTOOLS For .NET Class Library Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.28
Using Non Automated Annotations in Run Mode
Take the following steps to create and run a program that implements non-automated annotations, This program will let you create a Note and a button annotations, set the user mode through a menu, and activate the annotations by left clicking on the annotation.
  1. Start Visual Studio .NET.
  2. Choose File->New->Project… from the menu.
  3. In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
  4. Type the project name as "Using Non Automated Annotations in Run Mode" 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, and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to the "<LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32 " folder and select the following DLLs:
    • Leadtools.dll
    • Leadtools.Annotations.dll
    • Leadtools.Codecs.dll
    • Leadtools.Codecs.Cmp.dll
    • Leadtools.WinForms.dll
    Click the Select button and then press the OK button to add the above DLLs to the application.
  6. Make sure Form1 is in design view. Go to the toolbox (View->Toolbox) and Drag and drop an instance of RasterImageViewer to the form. If you do not have RasterImageViewer in your toolbox, select Tools->Add Remove Toolbox Items from the menu. Click Browse and then select Leadtools.WinForms.DLL from the "<LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32" folder and then click Open and then click OK.
  7. Go to the toolbox (View->Toolbox) and Drag and drop a 2 RadioButton controls on the form and set the following properties for them:
    Text Name Checked
    Design Mode radioButton1 True
    Run Mode radioButton2 False
  8. Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code ) and add the following lines at the beginning of the file:

    [Visual Basic]

                 
                Imports Leadtools
                Imports Leadtools.Annotations
                Imports Leadtools.Codecs
                Imports Leadtools.WinForms
                
    
    [C#]
                 
                using Leadtools;
                using Leadtools.Annotations;
                using Leadtools.Codecs;
                using Leadtools.WinForms;
                
    
  9. Declare the following private enumeration:

    [Visual Basic]

                 
                ' user mode
                Private Enum UserModeEnum
                   RunMode
                   DesignMode
                End Enum
                
    
    [C#]
                 
                // user mode
                private enum UserModeEnum
                {
                   RunMode,
                   DesignMode
                }
                
    
  10. Declare the following private variables:

    [Visual Basic]

                 
                ' Annotation container object
                Private annContainerObj As AnnContainer
                ' Current user mode
                Private currentUserMode As UserModeEnum
                
    
    [C#]
                 
                // Annotation container object
                private AnnContainer annContainerObj;
                // Current user mode
                private UserModeEnum currentUserMode;
                
    
  11. Add an event handler to the Form1 Load event and code it as follows:

    [Visual Basic]

                Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                   ' intitialize a new RasterCodecs
                   Dim codecs As New RasterCodecs()
                   ' load the main image into our viewer
                   rasterImageViewer1.Image = codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\Sample1.cmp")
                   
                   ' we are in design mode now
                   currentUserMode = UserModeEnum.DesignMode
                   
                   If (Not IsNothing(RasterImageViewer1.Image)) Then
                      ' initialize the AnnContainer object and associate it with rasterImageViewer1 image
                      annContainerObj = New AnnContainer
                      annContainerObj.Bounds = New AnnRectangle(0, 0, RasterImageViewer1.ImageSize.Width, RasterImageViewer1.ImageSize.Height, AnnUnit.Pixel)
                      annContainerObj.Name = "Container"
                      annContainerObj.Visible = True
                      annContainerObj.UnitConverter = New AnnUnitConverter(96, 96)
                      ' create Annotation Button Object and add it to the container
                      annContainerObj.Objects.Add(CreateAnnButtonObject(New AnnRectangle(1, 1, annContainerObj.Bounds.Right / 4, annContainerObj.Bounds.Bottom / 4)))
                      ' create Annotation Note Object and add it to the container
                      annContainerObj.Objects.Add(CreateAnnNoteObject(New AnnRectangle(annContainerObj.Bounds.Right / 2, annContainerObj.Bounds.Bottom / 2, (annContainerObj.Bounds.Right / 4) - 1, (annContainerObj.Bounds.Bottom / 4) - 1)))
                   End If
                End Sub   
                
    
    [C#]
                private void Form1_Load(object sender, System.EventArgs e)
                {
                   // intitialize a new RasterCodecs
                   RasterCodecs codecs = new RasterCodecs();
                   // load the main image into our viewer 
                   rasterImageViewer1.Image = codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\Sample1.cmp");
                   
                   // we are in design mode now
                   currentUserMode = UserModeEnum.DesignMode;
                   
                   if(rasterImageViewer1.Image != null)
                   {
                      // initialize the AnnContainer object and associate it with rasterImageViewer1 image
                      annContainerObj = new AnnContainer();
                      annContainerObj.Bounds = new AnnRectangle(0, 0, rasterImageViewer1.ImageSize.Width, rasterImageViewer1.ImageSize.Height, AnnUnit.Pixel);
                      annContainerObj.Name = "Container";
                      annContainerObj.Visible = true;
                      annContainerObj.UnitConverter = new AnnUnitConverter(96, 96);
                      // create Annotation Button Object and add it to the container
                      annContainerObj.Objects.Add(CreateAnnButtonObject(new AnnRectangle(1, 1, annContainerObj.Bounds.Right/4, annContainerObj.Bounds.Bottom/4)));
                      // create Annotation Note Object and add it to the container
                      annContainerObj.Objects.Add(CreateAnnNoteObject(new AnnRectangle(annContainerObj.Bounds.Right/2, annContainerObj.Bounds.Bottom/2, (annContainerObj.Bounds.Right/4) - 1, (annContainerObj.Bounds.Bottom/4) - 1)));
                   }
                }
                
    
  12. Add an event handler to the radioButton1 CheckedChanged event and code it as follows:

    [Visual Basic]

                Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
                   UserModeChanged(sender)
                End Sub   
                
    
    [C#]
                private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
                {
                   UserModeChanged(sender);
                }
                
    
  13. Add an event handler to the radioButton2 CheckedChanged event and code it as follows:

    [Visual Basic]

                 
                Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
                   UserModeChanged(sender)
                End Sub   
                
    
    [C#]
                 
                private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
                {
                   UserModeChanged(sender);
                }               
                
    
  14. Add an event handler to the rasterImageViewer1 MouseDown event and code it as follows:

    [Visual Basic]

                Private Sub RasterImageViewer1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RasterImageViewer1.MouseDown
                   If (IsNothing(RasterImageViewer1.Image)) Then
                      Return
                   End If
                   If (currentUserMode = UserModeEnum.RunMode) Then
                      Dim obj As AnnObject = HitTest(e.X, e.Y)
                      If ((Not IsNothing(obj)) AndAlso ((TypeOf (obj) Is AnnButtonObject) OrElse (TypeOf (obj) Is AnnNoteObject))) Then
                         If (TypeOf (obj) Is AnnButtonObject) Then
                            Dim buttonObj As AnnButtonObject = obj
                            buttonObj.Pushed = True
                            RasterImageViewer1.Invalidate(buttonObj.InvalidRectangle)
                            HandleHyperLink(buttonObj)
                            buttonObj.Pushed = False
                            RasterImageViewer1.Invalidate(buttonObj.InvalidRectangle)
                         Else
                            Dim annNoteObj As AnnNoteObject = obj
                            HandleHyperLink(annNoteObj)
                         End If
                      End If
                   End If
                End Sub   
                
    
    [C#]
                private void rasterImageViewer1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
                {
                   if(rasterImageViewer1.Image == null)
                      return;
                   if(currentUserMode == UserModeEnum.RunMode)
                   {
                      AnnObject obj = HitTest(e.X, e.Y);
                      if(obj != null && ((obj is AnnButtonObject) || (obj is AnnNoteObject)))
                      {
                         if(obj is AnnButtonObject)
                         {
                            AnnButtonObject buttonObj = obj as AnnButtonObject;
                            buttonObj.Pushed = true;
                               rasterImageViewer1.Invalidate(buttonObj.InvalidRectangle);
                            HandleHyperLink(buttonObj);
                            buttonObj.Pushed = false;
                               rasterImageViewer1.Invalidate(buttonObj.InvalidRectangle);
                         }
                         else
                         {
                            AnnNoteObject annNoteObj = obj as AnnNoteObject;
                            HandleHyperLink(annNoteObj);
                         }
                      }
                   }
                }
                
    
  15. Add an event handler to the rasterImageViewer1 TransformChanged event and code it as follows:

    [Visual Basic]

                 
                Private Sub RasterImageViewer1_TransformChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RasterImageViewer1.TransformChanged
                   If (Not IsNothing(annContainerObj)) Then
                      annContainerObj.Transform = RasterImageViewer1.Transform.Clone()
                   End If
                End Sub   
                
    
    [C#]
                private void rasterImageViewer1_TransformChanged(object sender, System.EventArgs e)
                {
                   if(annContainerObj != null)
                      annContainerObj.Transform = rasterImageViewer1.Transform.Clone();
                }
                
    
  16. Add an event handler to the rasterImageViewer1 PostImagePaint event and code it as follows:

    [Visual Basic]

                Private Sub RasterImageViewer1_PostImagePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles RasterImageViewer1.PostImagePaint
                   If (Not IsNothing(annContainerObj)) Then
                      annContainerObj.Draw(e.Graphics)
                   End If
                End Sub   
                
    
    [C#]
                private void rasterImageViewer1_PostImagePaint(object sender, System.Windows.Forms.PaintEventArgs e)
                {
                   if(annContainerObj != null)
                      annContainerObj.Draw(e.Graphics);
                } 
                
    
  17. Add the following functions code to class Form1:

    [Visual Basic]

                Private Sub UserModeChanged(ByVal sender As Object)
                   If (sender Is radioButton1) Then
                      currentUserMode = UserModeEnum.DesignMode
                   Else
                      currentUserMode = UserModeEnum.RunMode
                   End If
                End Sub
                Private Function CreateAnnButtonObject(ByVal boundingRect As AnnRectangle) As AnnObject
                   Dim annButtonObj As AnnButtonObject = New AnnButtonObject
                   annButtonObj.Text = "Click me"
                   annButtonObj.TextColor = Color.FromArgb(255, 0, 0)
                   annButtonObj.Pen = New AnnPen(Color.Red, New AnnLength(3))
                   annButtonObj.Brush = New AnnSolidBrush(Color.Yellow)
                   annButtonObj.Font = New AnnFont(New FontFamily("Arial"), New AnnLength(14), FontStyle.Bold)
                   annButtonObj.Hyperlink = "www.leadtools.com"
                   annButtonObj.Bounds = boundingRect
                   Return annButtonObj
                End Function
                Private Function CreateAnnNoteObject(ByVal boundingRect As AnnRectangle) As AnnObject
                   Dim annNoteObj As AnnNoteObject = New AnnNoteObject
                   annNoteObj.Text = "This is my Text"
                   annNoteObj.TextColor = Color.FromArgb(255, 0, 0)
                   annNoteObj.Pen = New AnnPen(Color.Red, New AnnLength(3))
                   annNoteObj.Brush = New AnnSolidBrush(Color.Yellow)
                   annNoteObj.Font = New AnnFont(New FontFamily("Arial"), New AnnLength(14), FontStyle.Bold)
                   annNoteObj.Hyperlink = "Notepad.exe"
                   annNoteObj.Bounds = boundingRect
                   Return annNoteObj
                End Function
                Private Function HitTest(ByVal x As Single, ByVal y As Single) As AnnObject
                   Dim hitTestBuffer As Integer = 2
                   Dim obj As AnnObject = Nothing
                   If (Not IsNothing(RasterImageViewer1.Image)) Then
                   
                      Dim testPoint As AnnPoint = New AnnPoint(x, y, AnnUnit.Pixel)
                      obj = annContainerObj.HitTest(testPoint, hitTestBuffer)
                   End If
                   
                   Return obj
                End Function
                   
                Private Sub HandleHyperLink(ByVal obj As AnnObject)
                   If (Not IsNothing(obj.Hyperlink)) Then
                      Try
                         System.Diagnostics.Process.Start(obj.Hyperlink)
                      Catch ex As Exception
                         MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                      End Try
                   End If
                End Sub
                
    
    [C#]
                private void UserModeChanged(object sender)
                {
                   currentUserMode = (sender == radioButton1)?UserModeEnum.DesignMode: UserModeEnum.RunMode;
                }
                private AnnObject CreateAnnButtonObject( AnnRectangle boundingRect)
                {
                   AnnButtonObject annButtonObj = new AnnButtonObject();
                   annButtonObj.Text = "Click me";
                   annButtonObj.TextColor = Color.FromArgb(255, 0, 0);
                   annButtonObj.Pen = new AnnPen(Color.Red , new AnnLength(3));
                   annButtonObj.Brush = new AnnSolidBrush(Color.Yellow);
                   annButtonObj.Font = new AnnFont(new FontFamily("Arial"), new AnnLength(14), FontStyle.Bold);
                   annButtonObj.Hyperlink = "www.leadtools.com";
                   annButtonObj.Bounds = boundingRect;
                   return annButtonObj;
                }
                private AnnObject CreateAnnNoteObject( AnnRectangle boundingRect)
                {
                   AnnNoteObject annNoteObj = new AnnNoteObject();
                   annNoteObj.Text = "This is my Text";
                   annNoteObj.TextColor = Color.FromArgb(255, 0, 0);
                   annNoteObj.Pen = new AnnPen(Color.Red , new AnnLength(3));
                   annNoteObj.Brush = new AnnSolidBrush(Color.Yellow);
                   annNoteObj.Font = new AnnFont(new FontFamily("Arial"), new AnnLength(14), FontStyle.Bold);
                   annNoteObj.Hyperlink = "Notepad.exe";
                   annNoteObj.Bounds = boundingRect;
                   return annNoteObj;
                }
                private AnnObject HitTest(float x, float y)
                {
                   int hitTestBuffer = 2;
                   AnnObject obj = null;
                   if(rasterImageViewer1.Image != null)
                   {
                      AnnPoint testPoint = new AnnPoint(x, y, AnnUnit.Pixel);
                      obj = annContainerObj.HitTest(testPoint, hitTestBuffer);
                   }
                   return obj;
                }
                private void HandleHyperLink(AnnObject obj)
                {
                   if(obj.Hyperlink != string.Empty)
                   {
                      try
                      {
                         System.Diagnostics.Process.Start(obj.Hyperlink);
                      }
                      catch(Exception ex)
                      {
                         MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                      }
                   }
                }
                
    
  18. Build and run the program to test it. Click on the Note and Button annotations. Since the User Mode is Design Mode, nothing happens. Select the "Run Mode" radio button then click on each annotation.