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.
Start Visual Studio .NET. Choose File->New->Project… from the menu. 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. 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 . 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 Leadtools For .NET "\LEAD Technologies\LEADTOOLS 16.5\Bin\DotNet\Win32 " folder and select the following DLLs: Leadtools.dll Leadtools.Annotations.dll Leadtools.Codecs.dll Leadtools.WinForms.dll
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 "C:\Program Files (x86)\LEAD Technologies\LEADTOOLS 16.5\Bin\DotNet\Win32" and then click Open and then click OK. Go to the toolbox (View->Toolbox) and Drag and drop a 2 instance of RadioButton control to the top of the form and set the following properties for them: Text Name Checked Design Mode radioButton1 True Run Mode radioButton2 False 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]
[C#]Imports Leadtools Imports Leadtools.Annotations Imports Leadtools.Codecs Imports Leadtools.WinForms
using Leadtools; using Leadtools.Annotations; using Leadtools.Codecs; using Leadtools.WinForms;
Declare the following private enumeration: [Visual Basic]
[C#]' user mode Private Enum UserModeEnum RunMode DesignMode End Enum
// user mode private enum UserModeEnum { RunMode, DesignMode }
Declare the following private variables: [Visual Basic]
[C#]' Annotation container object Private annContainerObj As AnnContainer ' Current user mode Private currentUserMode As UserModeEnum
// Annotation container object private AnnContainer annContainerObj; // Current user mode private UserModeEnum currentUserMode;
Add an event handler to the Form1 Load event and code it as follows: [Visual Basic]
[C#]Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' intitialize a new RasterCodecs RasterCodecs.Startup() Dim codecs As New RasterCodecs() ' load the main image into our viewer rasterImageViewer1.Image = codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\Sample1.cmp") RasterCodecs.Shutdown() ' 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
private void Form1_Load(object sender, System.EventArgs e) { // intitialize a new RasterCodecs RasterCodecs.Startup(); RasterCodecs codecs = new RasterCodecs(); // load the main image into our viewer rasterImageViewer1.Image = codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\Sample1.cmp"); RasterCodecs.Shutdown(); // 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))); } }
Add an event handler to the radioButton1 CheckedChanged event and code it as follows: [Visual Basic]
[C#]Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged UserModeChanged(sender) End Sub
private void radioButton1_CheckedChanged(object sender, System.EventArgs e) { UserModeChanged(sender); }
Add an event handler to the radioButton2 CheckedChanged event and code it as follows: [Visual Basic]
[C#]Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged UserModeChanged(sender) End Sub
private void radioButton2_CheckedChanged(object sender, System.EventArgs e) { UserModeChanged(sender); }
Add an event handler to the rasterImageViewer1 MouseDown event and code it as follows: [Visual Basic]
[C#]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
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); } } } }
Add an event handler to the rasterImageViewer1 TransformChanged event and code it as follows: [Visual Basic]
[C#]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
private void rasterImageViewer1_TransformChanged(object sender, System.EventArgs e) { if(annContainerObj != null) annContainerObj.Transform = rasterImageViewer1.Transform.Clone(); }
Add an event handler to the rasterImageViewer1 PostImagePaint event and code it as follows: [Visual Basic]
[C#]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
private void rasterImageViewer1_PostImagePaint(object sender, System.Windows.Forms.PaintEventArgs e) { if(annContainerObj != null) annContainerObj.Draw(e.Graphics); }
Add the following functions code to class Form1: [Visual Basic]
[C#]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
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); } } }
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 Run Mode Radio button then click on each annotation.