Take the following steps to create and run a program that implements non-automated annotations. You will create a Note annotation, set the user mode, and activate the annotation by left-clicking the annotation.
Start Visual Studio.
Choose File->New->Project… from the menu.
In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Forms Application" in the Templates List.
Type the project name as "Using Non-Automated Annotations in Run Mode" in the Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK.
In the "Solution Explorer" window, right-click 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\DotNet4\Win32" folder and select the following DLLs:
Click Add and then click OK to add the above DLLs to the application.
Make sure Form1 is in design view. Go to the toolbox (View->Toolbox) and drag and drop an instance of the ImageViewer on the form. If you do not have the ImageViewer in your toolbox, choose Tools->Choose Toolbox Items from the main menu. Click Browse and then select the Leadtools.WinForms.DLL from the "<LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32" folder. Next, click Open and then click OK.
Go to the toolbox (View->Toolbox) and drag and drop 2 Radio Button controls onto the form and set their properties as follows:
| Text | Name | Checked |
| Design Mode | radioButton1 | True |
| Run Mode | radioButton2 | False |
Switch to Form1 code view (right-click Form1 in the Solution Explorer and then select View Code). Add the following lines at the beginning of the file:
Imports LeadtoolsImports Leadtools.Annotations.EngineImports Leadtools.Annotations.RenderingImports Leadtools.CodecsImports Leadtools.WinForms
using Leadtools;using Leadtools.Annotations.Engine;using Leadtools.Annotations.Rendering;using Leadtools.Codecs;using Leadtools.WinForms;
Declare the following private enumeration:
' user modePrivate Enum UserModeEnumRunModeDesignModeEnd Enum
// user modeprivate enum UserModeEnum{RunMode,DesignMode}
Declare the following private variables:
' Annotation container objectPrivate annContainerObj As AnnContainer' Annotation Rendering EnginePrivate renderingEngine As AnnWinFormsRenderingEngine' Current user modePrivate currentUserMode As UserModeEnum
// Annotation container objectprivate AnnContainer annContainerObj;// Annotation Rendering Engineprivate AnnWinFormsRenderingEngine renderingEngine;// Current user modeprivate UserModeEnum currentUserMode;
Add an event handler to the Form1 Load event and code it as follows:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' initialize a new RasterCodecsDim codecs As New RasterCodecs()' load the main image into our viewerRasterImageViewer1.Image = codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\Sample1.cmp")RasterImageViewer1.Zoom(ControlSizeMode.Fit, RasterImageViewer1.ScaleFactor, RasterImageViewer1.DefaultZoomOrigin)' set the User Mode to design modecurrentUserMode = UserModeEnum.DesignModeIf (Not IsNothing(RasterImageViewer1.Image)) Then' initialize the AnnContainer object and associate it with rasterImageViewer1 imageannContainerObj = New AnnContainer' create an Annotation Note Object and add it to the containerannContainerObj.Children.Add(CreateAnnNoteObject(New LeadRectD(10, 10, 500, 500)))' initialize the Rendering EnginerenderingEngine = New AnnWinFormsRenderingEngine()End IfEnd Sub
private void Form1_Load(object sender, System.EventArgs e){// initialize a new RasterCodecsRasterCodecs codecs = new RasterCodecs();// load the main image into our viewerrasterImageViewer1.Image = codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\Sample1.cmp");rasterImageViewer1.Zoom(ControlSizeMode.Fit, rasterImageViewer1.ScaleFactor, rasterImageViewer1.DefaultZoomOrigin);// set the User Mode to Design ModecurrentUserMode = UserModeEnum.DesignMode;if(rasterImageViewer1.Image != null){// initialize the AnnContainer object and associate it with the rasterImageViewer1 imageannContainerObj = new AnnContainer();// create an Annotation Note Object and add it to the containerannContainerObj.Children.Add(CreateAnnNoteObject(new LeadRectD(10, 10, 500, 500)));// initialize the Rendering EnginerenderingEngine = new AnnWinFormsRenderingEngine();}}
Add an event handler to the radioButton1 CheckedChanged event and code it as follows:
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _Handles RadioButton1.CheckedChangedUserModeChanged(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:
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChangedUserModeChanged(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:
Private Sub RasterImageViewer1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RasterImageViewer1.MouseDownIf (IsNothing(RasterImageViewer1.Image)) ThenReturnEnd IfIf (currentUserMode = UserModeEnum.RunMode) ThenDim obj As AnnObject = HitTest(e.X, e.Y)If (Not IsNothing(obj)) ThenDim annNoteObj As AnnNoteObject = objIf (Not IsNothing(annNoteObj)) ThenHandleHyperLink(annNoteObj)End IfEnd IfEnd IfEnd 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){AnnNoteObject annNoteObj = obj as AnnNoteObject;if(annNoteObj != null)HandleHyperLink(annNoteObj);}}}
Add an event handler to the rasterImageViewer1 PostImagePaint event and code it as follows:
Private Sub RasterImageViewer1_PostImagePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles RasterImageViewer1.PostImagePaintIf (Not IsNothing(annContainerObj)) ThenrenderingEngine.Attach(annContainerObj, e.Graphics)renderingEngine.Render(LeadRectD.Empty, true)renderingEngine.Detach()End IfEnd Sub
private void rasterImageViewer1_PostImagePaint(object sender, System.Windows.Forms.PaintEventArgs e){if(annContainerObj != null){renderingEngine.Attach(annContainerObj, e.Graphics);renderingEngine.Render(LeadRectD.Empty, true);renderingEngine.Detach();}}
Add the following function code to class Form1:
Private Sub UserModeChanged(ByVal sender As Object)If (sender Is radioButton1) ThencurrentUserMode = UserModeEnum.DesignModeElsecurrentUserMode = UserModeEnum.RunModeEnd IfEnd SubPrivate Function CreateAnnNoteObject(ByVal boundingRect As LeadRectD) As AnnObjectDim annNoteObj As AnnNoteObject = New AnnNoteObjectannNoteObj.Text = "This is my Text"annNoteObj.Font = New AnnFont("Arial", 14)annNoteObj.Hyperlink = "Notepad.exe"annNoteObj.Rect = boundingRectReturn annNoteObjEnd FunctionPrivate Function HitTest(ByVal x As Single, ByVal y As Single) As AnnObjectDim obj As AnnObject = NothingIf (Not IsNothing(RasterImageViewer1.Image)) ThenDim testPoint As LeadPointD = New LeadPointD(x, y)Dim objects As Array = annContainerObj.HitTestPoint(testPoint)If objects.Length > 0 Thenobj = objects(0)End IfEnd IfReturn objEnd FunctionPrivate Sub HandleHyperLink(ByVal obj As AnnObject)If (Not IsNothing(obj.Hyperlink)) ThenTrySystem.Diagnostics.Process.Start(obj.Hyperlink)Catch ex As ExceptionMessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)End TryEnd IfEnd Sub
private void UserModeChanged(object sender){currentUserMode = (sender == radioButton1)?UserModeEnum.DesignMode: UserModeEnum.RunMode;}private AnnObject CreateAnnNoteObject(LeadRectD boundingRect){AnnNoteObject annNoteObj = new AnnNoteObject();annNoteObj.Text = "This is my Text";annNoteObj.Font = new AnnFont("Arial", 14);annNoteObj.Hyperlink = "Notepad.exe";annNoteObj.Rect = boundingRect;return annNoteObj;}private AnnObject HitTest(float x, float y){AnnObject obj = null;if (rasterImageViewer1.Image != null){LeadPointD testPoint = new LeadPointD(x, y);AnnObject[] objects = annContainerObj.HitTestPoint(testPoint);if (objects.Length > 0)obj = objects[0];}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);}}}