Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Runtime.Serialization
Imports System.Security.Permissions
Imports Vintasoft.Imaging
Imports Vintasoft.Imaging.Annotation
Imports Vintasoft.Imaging.Annotation.Rendering
Namespace CommonCode.Annotation
''' <summary>
''' Class that holds information about the annotation that displays a mark.
''' </summary>
<Serializable> _
Public Class MarkAnnotationData
Inherits AnnotationData
#Region "Constructors"
''' <summary>
''' Initializes a new instance of the <see cref="MarkAnnotationData"/> class.
''' </summary>
Public Sub New()
MyBase.New()
FillBrush = New AnnotationSolidBrush(Color.Black)
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="AnnotationData"/> class.
''' </summary>
''' <param name="info">The SerializationInfo to populate with data.</param>
''' <param name="context">The destination for this serialization.</param>
Public Sub New(info As SerializationInfo, context As StreamingContext)
MyBase.New(info, context)
_markType = CType(info.GetValue("MarkType", GetType(Integer)), MarkAnnotationType)
End Sub
''' <summary>
''' Initializes the <see cref="MarkAnnotationData"/> class.
''' </summary>
Shared Sub New()
' register renderer form this annotation
AnnotationRendererFactory.RegisterRendererForAnnotationData(GetType(MarkAnnotationData), GetType(MarkAnnotationRenderer))
End Sub
#End Region
#Region "Properties"
Private _markType As MarkAnnotationType = MarkAnnotationType.Tick
''' <summary>
''' Gets or sets a mark type.
''' </summary>
<Description("The mark type.")> _
<DefaultValue(MarkAnnotationType.Tick)> _
Public Property MarkType() As MarkAnnotationType
Get
Return _markType
End Get
Set
If _markType <> value Then
Dim changingArgs As New ObjectPropertyChangingEventArgs("MarkType", _markType, value)
If OnPropertyChanging(changingArgs) Then
_markType = CType(changingArgs.NewValue, MarkAnnotationType)
OnPropertyChanged(changingArgs.ToChangedEventArgs())
End If
End If
End Set
End Property
#End Region
#Region "Methods"
''' <summary>
''' Returns the bounding box of annotation if annotation will have specified location,
''' size and rotation.
''' </summary>
''' <param name="location">Location, in device-independent pixels (1/96th inch),
''' of annotation.</param>
''' <param name="size">Size, in device-independent pixels (1/96th inch),
''' of annotation</param>
''' <param name="rotation">Rotation, in degrees, of annotation.</param>
''' <returns>Bounding box of annotation.</returns>
Public Overrides Function GetBoundingBox(location As PointF, size As SizeF, rotation__1 As Single) As RectangleF
Dim points As PointF() = GetReferencePointsInContentSpace()
' rotate
AnnotationsMath.RotatePointsAt(points, PointF.Empty, Rotation)
' scale
AnnotationsMath.ScalePoints(points, size.Width / Me.Size.Width, size.Height / Me.Size.Height)
' translate
AnnotationsMath.TranslatePoints(points, location.X, location.Y)
Return AnnotationsMath.GetBoundingBox(points)
End Function
''' <summary>
''' Gets an array that contains reference points in content space of this annotation.
''' </summary>
''' <returns></returns>
Public Overridable Function GetReferencePointsInContentSpace() As PointF()
Dim width As Single = Size.Width
Dim height As Single = Size.Height
Dim w As Single = Math.Min(width / 10, height / 10)
Dim points As PointF()
Select Case MarkType
Case MarkAnnotationType.Rectangle
points = New PointF() {New PointF(-width / 2, -height / 2), New PointF(width / 2, -height / 2), New PointF(width / 2, height / 2), New PointF(-width / 2, height / 2)}
Exit Select
Case MarkAnnotationType.Tick
points = New PointF() {New PointF(-width / 2, 0), New PointF(0, height / 4), New PointF(width / 2, -height / 2), New PointF(0, height / 2), New PointF(-width / 2, 0)}
Exit Select
Case MarkAnnotationType.Cross
points = New PointF() {New PointF(-width / 2, -w), New PointF(-w, -w), New PointF(-w, -height / 2), New PointF(w, -height / 2), New PointF(w, -w), New PointF(width / 2, -w), _
New PointF(width / 2, w), New PointF(w, w), New PointF(w, height / 2), New PointF(-w, height / 2), New PointF(-w, w), New PointF(-width / 2, w)}
Exit Select
Case MarkAnnotationType.Star
points = New PointF() {New PointF(-width / 2, 0), New PointF(-w, -w), New PointF(0, -height / 2), New PointF(w, -w), New PointF(width / 2, 0), New PointF(w, w), _
New PointF(0, height / 2), New PointF(-w, w), New PointF(-width / 2, 0)}
Exit Select
Case Else
Throw New NotImplementedException()
End Select
Dim matrix As New AffineMatrix()
If HorizontalMirrored Then
matrix.ScalePrepend(-1, 1)
End If
If VerticalMirrored Then
matrix.ScalePrepend(1, -1)
End If
PointFAffineTransform.TransformPoints(matrix, points)
Return points
End Function
''' <summary>
''' Populates a SerializationInfo with the data needed to serialize the target object.
''' </summary>
''' <param name="info">The SerializationInfo to populate with data.</param>
''' <param name="context">The destination for this serialization.</param>
<SecurityPermission(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.SerializationFormatter)> _
Public Overrides Sub GetObjectData(info As SerializationInfo, context As StreamingContext)
MyBase.GetObjectData(info, context)
info.AddValue("MarkType", CInt(MarkType))
End Sub
''' <summary>
''' Creates a new object that is a copy of the current instance.
''' </summary>
''' <returns>A new object that is a copy of this instance.</returns>
Public Overrides Function Clone() As Object
Dim data As New MarkAnnotationData()
CopyTo(data)
Return data
End Function
''' <summary>
''' Copies the state of the current object to the target object.
''' </summary>
''' <param name="target">Object to copy the state of the current object to.</param>
Public Overrides Sub CopyTo(target As AnnotationData)
MyBase.CopyTo(target)
If TypeOf target Is MarkAnnotationData Then
DirectCast(target, MarkAnnotationData).MarkType = MarkType
End If
End Sub
#End Region
End Class
End Namespace