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 Leadtools
Imports Leadtools.Annotations.Engine
Imports Leadtools.Annotations.Rendering
Imports Leadtools.Codecs
Imports Leadtools.WinForms
using Leadtools;
using Leadtools.Annotations.Engine;
using Leadtools.Annotations.Rendering;
using Leadtools.Codecs;
using Leadtools.WinForms;
Declare the following private enumeration:
' user mode
Private Enum UserModeEnum
RunMode
DesignMode
End Enum
// user mode
private enum UserModeEnum
{
RunMode,
DesignMode
}
Declare the following private variables:
' Annotation container object
Private annContainerObj As AnnContainer
' Annotation Rendering Engine
Private renderingEngine As AnnWinFormsRenderingEngine
' Current user mode
Private currentUserMode As UserModeEnum
// Annotation container object
private AnnContainer annContainerObj;
// Annotation Rendering Engine
private AnnWinFormsRenderingEngine renderingEngine;
// Current user mode
private 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 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")
RasterImageViewer1.Zoom(ControlSizeMode.Fit, RasterImageViewer1.ScaleFactor, RasterImageViewer1.DefaultZoomOrigin)
' set the User Mode to design mode
currentUserMode = UserModeEnum.DesignMode
If (Not IsNothing(RasterImageViewer1.Image)) Then
' initialize the AnnContainer object and associate it with rasterImageViewer1 image
annContainerObj = New AnnContainer
' create an Annotation Note Object and add it to the container
annContainerObj.Children.Add(CreateAnnNoteObject(New LeadRectD(10, 10, 500, 500)))
' initialize the Rendering Engine
renderingEngine = New AnnWinFormsRenderingEngine()
End If
End Sub
private void Form1_Load(object sender, System.EventArgs e)
{
// initialize 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");
rasterImageViewer1.Zoom(ControlSizeMode.Fit, rasterImageViewer1.ScaleFactor, rasterImageViewer1.DefaultZoomOrigin);
// set the User Mode to Design Mode
currentUserMode = UserModeEnum.DesignMode;
if(rasterImageViewer1.Image != null)
{
// initialize the AnnContainer object and associate it with the rasterImageViewer1 image
annContainerObj = new AnnContainer();
// create an Annotation Note Object and add it to the container
annContainerObj.Children.Add(CreateAnnNoteObject(new LeadRectD(10, 10, 500, 500)));
// initialize the Rendering Engine
renderingEngine = 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.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:
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:
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)) Then
Dim annNoteObj As AnnNoteObject = obj
If (Not IsNothing(annNoteObj)) Then
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)
{
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.PostImagePaint
If (Not IsNothing(annContainerObj)) Then
renderingEngine.Attach(annContainerObj, e.Graphics)
renderingEngine.Render(LeadRectD.Empty, true)
renderingEngine.Detach()
End If
End 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) Then
currentUserMode = UserModeEnum.DesignMode
Else
currentUserMode = UserModeEnum.RunMode
End If
End Sub
Private Function CreateAnnNoteObject(ByVal boundingRect As LeadRectD) As AnnObject
Dim annNoteObj As AnnNoteObject = New AnnNoteObject
annNoteObj.Text = "This is my Text"
annNoteObj.Font = New AnnFont("Arial", 14)
annNoteObj.Hyperlink = "Notepad.exe"
annNoteObj.Rect = boundingRect
Return annNoteObj
End Function
Private Function HitTest(ByVal x As Single, ByVal y As Single) As AnnObject
Dim obj As AnnObject = Nothing
If (Not IsNothing(RasterImageViewer1.Image)) Then
Dim testPoint As LeadPointD = New LeadPointD(x, y)
Dim objects As Array = annContainerObj.HitTestPoint(testPoint)
If objects.Length > 0 Then
obj = objects(0)
End If
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 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);
}
}
}