Home : Annotations : Custom : Create a Custom Point Annotation
Q10087 - HOWTO: Create a Custom Point Annotation

DotImage does not include a "Point" annotation out of the box.  This topic discusses how to create a custom Point annotation in Visual Basic.

First, we create the PointAnnotation class.  To make life easier, we will derive from FrameableAnnotation, override OnPaint to draw the cross, and override Grips becase we do not want to show any grips when the point is selected.  We also add a Point property that returns the center of the annotation, calculated from the Location and Size.

Here is VB.NET code for the PointAnnotation:

Imports Atalasoft.Imaging.Annotate
Imports System.Drawing

Public Class PointAnnotation
    Inherits FrameableAnnotation
    Implements IAnnotationPen

    Dim _pen As System.Drawing.Pen = New Pen(Color.Black)
    Dim _grips As PointGrips = New PointGrips
    Const CROSSSIZE As Integer = 25

    Public Overloads Overrides Sub Paint(ByVal graphics As Graphics, ByVal offset As PointF, ByVal scale As PointF, ByVal resolution As Single)
        graphics.TranslateTransform(offset.X, offset.Y)
        graphics.ScaleTransform(scale.X, scale.Y)
        graphics.DrawLine(_pen, Location.X, Location.Y, Location.X + Size.Width, Location.Y + Size.Height)
        graphics.DrawLine(_pen, Location.X, Location.Y + Size.Height, Location.X + Size.Width, Location.Y)
        graphics.DrawEllipse(_pen, New RectangleF(Location, Size))
        graphics.ResetTransform()
    End Sub

    Public Property Pen() As Pen Implements Atalasoft.Imaging.Annotate.IAnnotationPen.Pen
        Get
            Return _pen
        End Get
        Set(ByVal Value As Pen)
            _pen = Value
        End Set
    End Property

    Public Property Point() As PointF
        Get
            Return New PointF((Location.X + Location.X + Size.Width) / 2, (Location.Y + Location.Y + Size.Height) / 2)
        End Get
        Set(ByVal Value As PointF)
            Location = New PointF(Value.X - Size.Width / 2, Value.Y - Size.Height / 2)
        End Set
    End Property

    Public Sub New()
        Size = New SizeF(CROSSSIZE, CROSSSIZE)
        Me.Resizable = False
    End Sub

    Public Sub New(ByVal point As PointF)
        Size = New SizeF(CROSSSIZE, CROSSSIZE)
        Me.Point = point
        Me.Resizable = False
    End Sub

    Public Overrides ReadOnly Property Grips() As Atalasoft.Imaging.Annotate.AnnotationGrips
        Get
            Return _grips
        End Get
    End Property
End Class

Now, in order to create the annotation, we do not want to use the "CreateAnnotation" method since that requires clicking and dragging to interactively position and size.  We just want the user to click on the document to place the point.  By handling the AnnotationController or AnnotateViewer's MouseDown event, we can simple add the new annotation to the Current Layer.  We do have to create a HitTest method that will loop through all annotations and make sure we're not clicking on an existing one.  This method will eventually be part of the AnnotationController class.

    Private Sub AnnotateViewer1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles AnnotateViewer1.MouseDown
        'place point annotation if we're not clicking on an existing one
        If HitTest(New PointF(e.X, e.Y)) Is Nothing Then
            'scale and offset the point
            Dim point As PointF = New PointF(e.X / Me.AnnotateViewer1.Zoom + Me.AnnotateViewer1.Location.X, e.Y / Me.AnnotateViewer1.Zoom + Me.AnnotateViewer1.Location.Y)
            Me.AnnotateViewer1.Annotations.CurrentLayer.Add(New PointAnnotation(Point))
        End If
    End Sub

    'make our own "HitTest" method to determine if the point is over an annotation
    Private Function HitTest(ByVal point As PointF) As Annotation
        For Each layer As Layer In AnnotateViewer1.Annotations.Layers
            For Each an As Annotation In layer
                Dim reg As Region = an.GetRegion()
                If reg.IsVisible(point.X, point.Y) Then
                    Return an
                End If
            Next
        Next
    End Function

If you wish to serialize the custom annotation in order to save it to XMP, you will also need to override the ToXmp and FromXmp methods.  See the documentation on how to do that.

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2017. InstantASP Ltd. All Rights Reserved