Files
2025-07-24 09:40:57 +02:00

859 lines
28 KiB
VB.net

Imports System.Drawing.Imaging
Imports System.Drawing.Font
<System.Runtime.InteropServices.ComVisible(False)> _
Public Class DrawingBoard
'Public Events
Public Event SetScrollPositions()
Public Event PointSelected(n As Integer)
Public Event PointUnselected()
'Member Variables
Private m_PanButton As System.Windows.Forms.MouseButtons = Windows.Forms.MouseButtons.Middle
Private m_EditButton As System.Windows.Forms.MouseButtons = Windows.Forms.MouseButtons.Left
Private m_OriginalImage As System.Drawing.Bitmap
Private m_StartPoint As System.Drawing.Point
Private m_Origin As New System.Drawing.Point(0, 0)
Private SrcRect As System.Drawing.Rectangle
Private DestRect As System.Drawing.Rectangle
Private m_ZoomOnMouseWheel As Boolean = True
Private m_ZoomFactor As Double = 1.0
Private m_ApparentImageSize As New Size(0, 0)
Private m_DrawWidth As Integer
Private m_DrawHeight As Integer
Private m_centerpoint As Point
Private m_PanMode As Boolean = True
Private m_StretchImageToFit As Boolean = True
Private m_Select_Rect As Rectangle
Private m_Select_Pen As New Pen(Color.Blue, 2) ' Pen used to indicate a selection on the image (zoom window)
Private EndPoint As Point ' for pan and box-zoom
' 4 punti di precalibrazione
Private m_pCaliCorner() As System.Drawing.Point
Private m_pCaliCornerH() As System.Drawing.Point
' per la calibrazione delle altezze
' per ora lo tengo
Private m_nPuntiCaliH As Integer
Private m_pCaliH() As System.Drawing.Point
Private m_bVisPuntiCaliH As Boolean
Private m_bVisPuntiCorner As Boolean
Private m_bPointSelected As Integer
Private m_isel As Integer, m_jsel As Integer
Private m_bCkRow As Boolean, m_bCkCol As Boolean
Private m_MouseEnabled As Boolean
Private Const std_radius As Integer = 40
Private Const small_radius As Integer = 30
Private Const dA_radius As Integer = 20
'Private m_MousePosImg As PointF
Private Enum ptype
PuntoCorner
PuntoCaliH
PuntoArea ' area generica
End Enum
Private m_ptypesel As ptype
Event NewMousePosImage(pm As PointF)
' gestione aree lastra (checkEsposizione, CheckStone, Def Ventosa)
' uso un'area generica rettangolare
Private m_bVisAreaGen As Boolean
Private m_pArea(3) As System.Drawing.Point
#Region "Public/Private Shadows"
Public Shadows Property Image() As System.Drawing.Image
Get
Return m_OriginalImage
End Get
Set(ByVal Value As System.Drawing.Image)
If Not m_OriginalImage Is Nothing Then
m_Select_Rect = Nothing
m_Origin = New Point(0, 0)
m_ApparentImageSize = New Size(0, 0)
m_ZoomFactor = 1
GC.Collect()
'GC.GetTotalMemory(True)
End If
If Value Is Nothing Then
m_OriginalImage = Nothing
Me.Invalidate()
Exit Property
End If
GC.Collect()
'Dim totalmem As Long = GC.GetTotalMemory(False)
'Dim r As New Rectangle(0, 0, Value.Width, Value.Height)
m_OriginalImage = Value
Me.Invalidate()
End Set
End Property
Public Shadows Property initialimage() As System.Drawing.Image
Get
Return m_OriginalImage
End Get
Set(ByVal value As System.Drawing.Image)
Me.Image = value
Me.ZoomFactor = 1
End Set
End Property
Public Shadows Property BackgroundImage() As System.Drawing.Image
Get
Return Nothing
End Get
Set(ByVal Value As System.Drawing.Image)
Me.Image = Value
Me.ZoomFactor = 1
End Set
End Property
#End Region
Public Property VisPuntiCorner() As Boolean
Get
Return m_bVisPuntiCorner
End Get
Set(ByVal value As Boolean)
m_bVisPuntiCorner = value
End Set
End Property
Public Property VisPuntiCaliH() As Boolean
Get
Return m_bVisPuntiCaliH
End Get
Set(ByVal value As Boolean)
m_bVisPuntiCaliH = value
End Set
End Property
Public WriteOnly Property CkRow() As Boolean
Set(ByVal value As Boolean)
m_bCkRow = value
End Set
End Property
Public WriteOnly Property CkCol() As Boolean
Set(ByVal value As Boolean)
m_bCkCol = value
End Set
End Property
Public Property MouseEnaBled() As Boolean
Get
Return m_MouseEnabled
End Get
Set(ByVal value As Boolean)
m_MouseEnabled = value
If Not value Then
m_bPointSelected = 0
End If
End Set
End Property
Public Property VisAreaGen() As Boolean
Get
Return m_bVisAreaGen
End Get
Set(ByVal value As Boolean)
m_bVisAreaGen = value
End Set
End Property
#Region "Protected Overrides"
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.Clear(Me.BackColor)
DrawImage(e.Graphics)
MyBase.OnPaint(e)
End Sub
Protected Overrides Sub OnSizeChanged(ByVal e As EventArgs)
DestRect = New System.Drawing.Rectangle(0, 0, ClientSize.Width, ClientSize.Height)
ComputeDrawingArea()
MyBase.OnSizeChanged(e)
End Sub
#End Region
#Region "Public Properties"
Public Sub ZoomIn()
ZoomImage(True)
End Sub
Public Sub ZoomOut()
ZoomImage(False)
End Sub
Private Sub ZoomImage(ByVal ZoomIn As Boolean)
' Get center point
m_centerpoint.X = m_Origin.X + SrcRect.Width / 2
m_centerpoint.Y = m_Origin.Y + SrcRect.Height / 2
'set new zoomfactor
If ZoomIn Then
ZoomFactor = Math.Round(ZoomFactor * 1.1, 2)
Else
ZoomFactor = Math.Round(ZoomFactor * 0.9, 2)
End If
'Reset the origin to maintain center point
m_Origin.X = CDbl(m_centerpoint.X) - (CDbl(ClientSize.Width) / m_ZoomFactor) / 2.0
m_Origin.Y = CDbl(m_centerpoint.Y) - (CDbl(ClientSize.Height) / m_ZoomFactor) / 2.0
CheckBounds()
End Sub
Private Sub ZoomImage(ByVal ZoomIn As Boolean, origin As Point)
' ZoomImage(ZoomIn)
' Return
' Coordinate origine scalate
Dim X_ As Integer = m_Origin.X * m_ZoomFactor
Dim Y_ As Integer = m_Origin.Y * m_ZoomFactor
' Coordinate cursore riferite all'origine immagine
Dim W_ As Integer = X_ + origin.X
Dim H_ As Integer = Y_ + origin.Y
' Dimensione attuale immagine
Dim pre_W As Integer = m_ApparentImageSize.Width
Dim pre_H As Integer = m_ApparentImageSize.Height
If pre_W < 0.001 And pre_H < 0.001 Then Return
'set new zoomfactor
If ZoomIn Then
ZoomFactor = Math.Round(ZoomFactor * 1.1, 2)
Else
ZoomFactor = Math.Round(ZoomFactor * 0.9, 2)
End If
' Nuove coordinate origine scalate
Dim X_n As Integer = m_Origin.X * m_ZoomFactor
Dim Y_n As Integer = m_Origin.Y * m_ZoomFactor
' Nuova dimensione attuale immagine
Dim post_W As Integer = m_ApparentImageSize.Width
Dim post_H As Integer = m_ApparentImageSize.Height
' Fattore di scalatura dell'immagine
Dim W_fac As Double = pre_W / post_W
Dim H_fac As Double = pre_H / post_H
' Nuove coordinate cursore riferite all'origine immagine
Dim W_n As Integer = W_ / W_fac
Dim H_n As Integer = H_ / H_fac
' Calcolo posizione origine per manatenre l'immagine fissa sulla posizione cursore
m_Origin.X += (-W_ + W_n + X_ - X_n) / m_ZoomFactor
m_Origin.Y += (-H_ + H_n + Y_ - Y_n) / m_ZoomFactor
End Sub
Public Sub InvertColors()
Try
Cursor = Cursors.WaitCursor
If m_OriginalImage Is Nothing Then Exit Sub
' This is the color matrix to invert the image colors.
Dim cm As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {-1, 0, 0, 0, 0}, _
New Single() {0, -1, 0, 0, 0}, _
New Single() {0, 0, -1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {1, 1, 1, 1, 1}})
Dim ImageAttributes As New ImageAttributes()
ImageAttributes.SetColorMatrix(cm)
Dim g As Graphics
g = Graphics.FromImage(m_OriginalImage)
Dim rc As New Rectangle(0, 0, m_OriginalImage.Width, m_OriginalImage.Height)
g.DrawImage(m_OriginalImage, rc, 0, 0, m_OriginalImage.Width, m_OriginalImage.Height, GraphicsUnit.Pixel, ImageAttributes)
Me.Invalidate()
Catch ex As Exception
Throw ex
Finally
Cursor = Cursors.Default
End Try
End Sub
Public Property PanButton() As System.Windows.Forms.MouseButtons
Get
Return m_PanButton
End Get
Set(ByVal value As System.Windows.Forms.MouseButtons)
m_PanButton = value
End Set
End Property
Public Property ZoomOnMouseWheel() As Boolean
Get
Return m_ZoomOnMouseWheel
End Get
Set(ByVal value As Boolean)
m_ZoomOnMouseWheel = value
End Set
End Property
Public Property ZoomFactor() As Double
Get
Return m_ZoomFactor
End Get
Set(ByVal value As Double)
m_ZoomFactor = value
If m_ZoomFactor > 15 Then m_ZoomFactor = 15
If m_ZoomFactor < 0.05 Then m_ZoomFactor = 0.05
If Not m_OriginalImage Is Nothing Then
m_ApparentImageSize.Height = m_OriginalImage.Height * m_ZoomFactor
m_ApparentImageSize.Width = m_OriginalImage.Width * m_ZoomFactor
ComputeDrawingArea()
CheckBounds()
End If
Me.Invalidate()
End Set
End Property
Public Property Origin() As System.Drawing.Point
Get
Return m_Origin
End Get
Set(ByVal value As System.Drawing.Point)
m_Origin = value
Me.Invalidate()
End Set
End Property
Public ReadOnly Property ApparentImageSize() As System.Drawing.Size
Get
Return m_ApparentImageSize
End Get
End Property
Public Property PanMode() As Boolean
Get
Return m_PanMode
End Get
Set(ByVal value As Boolean)
m_PanMode = value
End Set
End Property
Public Property StretchImageToFit() As Boolean
Get
Return m_StretchImageToFit
End Get
Set(ByVal value As Boolean)
m_StretchImageToFit = value
Me.Invalidate()
End Set
End Property
Public Sub fittoscreen()
Me.StretchImageToFit = False
Me.Origin = New Point(0, 0)
If m_OriginalImage Is Nothing Then Exit Sub
ZoomFactor = Math.Min(ClientSize.Width / m_OriginalImage.Width, ClientSize.Height / m_OriginalImage.Height)
End Sub
#End Region
Private Property Selected_Rectangle() As Rectangle
Get
Return m_Select_Rect
End Get
Set(ByVal Value As Rectangle)
m_Select_Rect = Value
Me.Invalidate()
End Set
End Property
Private Sub Draw_Rectangle(ByVal e As System.Windows.Forms.MouseEventArgs)
If (New Rectangle(0, 0, ClientSize.Width, ClientSize.Height)).Contains(PointToClient(Windows.Forms.Cursor.Position)) Then
Dim Width As Integer = System.Math.Abs(m_StartPoint.X - e.X)
Dim Height As Integer = System.Math.Abs(m_StartPoint.Y - e.Y)
Dim UpperLeft As Point
'need to determine the upper left corner of the rectangel regardless of whether it's
'the start point or the end point, or other.
UpperLeft = New Point(System.Math.Min(m_StartPoint.X, e.X), System.Math.Min(m_StartPoint.Y, e.Y))
Selected_Rectangle = New Rectangle(UpperLeft.X, UpperLeft.Y, Width, Height)
End If
End Sub
Private Sub DrawImage(ByRef g As Graphics)
If m_OriginalImage Is Nothing Then Exit Sub
Dim Font1 As New Font("Arial", 10)
g.PixelOffsetMode = Drawing2D.PixelOffsetMode.Half
g.SmoothingMode = Drawing2D.SmoothingMode.None
g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
If m_StretchImageToFit Then
SrcRect = New System.Drawing.Rectangle(0, 0, m_OriginalImage.Width, m_OriginalImage.Height)
Else
SrcRect = New System.Drawing.Rectangle(m_Origin.X, m_Origin.Y, m_DrawWidth, m_DrawHeight)
End If
Try
g.DrawImage(m_OriginalImage, DestRect, SrcRect, GraphicsUnit.Pixel)
Catch
End Try
If Not PanMode Then
g.DrawRectangle(m_Select_Pen, Selected_Rectangle)
End If
Dim i As Integer, j As Integer
Dim ix As Integer, iy As Integer
Dim ix2 As Integer, iy2 As Integer
If m_bVisPuntiCorner Then
For i = 0 To 3
ix = -m_Origin.X * m_ZoomFactor + m_pCaliCorner(i).X * m_ZoomFactor - std_radius
iy = -m_Origin.Y * m_ZoomFactor + m_pCaliCorner(i).Y * m_ZoomFactor - std_radius
If m_ptypesel = ptype.PuntoCorner AndAlso i = m_isel Then
g.DrawArc(Pens.GreenYellow, ix, iy, std_radius * 2, std_radius * 2, 0, 360)
Else
g.DrawArc(Pens.Beige, ix, iy, std_radius * 2, std_radius * 2, 0, 360)
End If
g.DrawString(i + 1, Font1, Brushes.Yellow, ix + 5, iy + 5)
Next
For i = 0 To 3
ix = -m_Origin.X * m_ZoomFactor + m_pCaliCorner(i).X * m_ZoomFactor
iy = -m_Origin.Y * m_ZoomFactor + m_pCaliCorner(i).Y * m_ZoomFactor
j = i + 1
If j = 4 Then j = 0
ix2 = -m_Origin.X * m_ZoomFactor + m_pCaliCorner(j).X * m_ZoomFactor
iy2 = -m_Origin.Y * m_ZoomFactor + m_pCaliCorner(j).Y * m_ZoomFactor
g.DrawLine(Pens.Beige, ix, iy, ix2, iy2)
Next
End If
If m_bVisPuntiCaliH Then
Dim raggio As Integer
For i = 0 To 3
If i = 0 Or i = 2 Then raggio = std_radius Else raggio = small_radius
ix = -m_Origin.X * m_ZoomFactor + m_pCaliH(i).X * m_ZoomFactor - raggio
iy = -m_Origin.Y * m_ZoomFactor + m_pCaliH(i).Y * m_ZoomFactor - raggio
g.DrawArc(Pens.Beige, ix, iy, raggio * 2, raggio * 2, 0, 360)
Next
For i = 0 To 2 Step 2
ix = -m_Origin.X * m_ZoomFactor + m_pCaliH(i).X * m_ZoomFactor
iy = -m_Origin.Y * m_ZoomFactor + m_pCaliH(i).Y * m_ZoomFactor
ix2 = -m_Origin.X * m_ZoomFactor + m_pCaliH(i + 1).X * m_ZoomFactor
iy2 = -m_Origin.Y * m_ZoomFactor + m_pCaliH(i + 1).Y * m_ZoomFactor
g.DrawLine(Pens.Beige, ix, iy, ix2, iy2)
Next
End If
If m_bVisAreaGen Then
' 4 cerchi
For i = 0 To 3 Step 2
ix = -m_Origin.X * m_ZoomFactor + m_pArea(i).X * m_ZoomFactor - dA_radius
iy = -m_Origin.Y * m_ZoomFactor + m_pArea(i).Y * m_ZoomFactor - dA_radius
g.DrawArc(Pens.GreenYellow, ix, iy, dA_radius * 2, dA_radius * 2, 0, 360)
Next
' 4 linee
For i = 0 To 3
ix = -m_Origin.X * m_ZoomFactor + m_pArea(i).X * m_ZoomFactor
iy = -m_Origin.Y * m_ZoomFactor + m_pArea(i).Y * m_ZoomFactor
j = i + 1
If j = 4 Then j = 0
ix2 = -m_Origin.X * m_ZoomFactor + m_pArea(j).X * m_ZoomFactor
iy2 = -m_Origin.Y * m_ZoomFactor + m_pArea(j).Y * m_ZoomFactor
g.DrawLine(Pens.Beige, ix, iy, ix2, iy2)
Next
End If
RaiseEvent SetScrollPositions()
End Sub
Private Sub ComputeDrawingArea()
'm_DrawHeight = Me.Height / m_ZoomFactor
'm_DrawWidth = Me.Width / m_ZoomFactor
m_DrawHeight = ClientSize.Height / m_ZoomFactor
m_DrawWidth = ClientSize.Width / m_ZoomFactor
End Sub
Private Sub ImageViewer_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If m_OriginalImage Is Nothing Then Exit Sub
If Not m_MouseEnabled Then Exit Sub
EndPoint = Nothing
Selected_Rectangle = Nothing
Dim i As Integer, previusSelection As Integer
Dim previusPtypeSel As ptype
Dim px As Double, py As Double
'Set the start point. This is used for panning and zooming so we always set it.
m_StartPoint = New Point(e.X, e.Y)
previusSelection = m_bPointSelected
m_bPointSelected = 0
previusPtypeSel = m_ptypesel
If m_bVisPuntiCorner Then
For i = 0 To 3
px = -m_Origin.X * m_ZoomFactor + m_pCaliCorner(i).X * m_ZoomFactor
py = -m_Origin.Y * m_ZoomFactor + m_pCaliCorner(i).Y * m_ZoomFactor
If System.Math.Abs(px - e.X) < std_radius And System.Math.Abs(py - e.Y) < std_radius Then
m_bPointSelected = 1
m_isel = i
m_jsel = 0
i = 3
m_ptypesel = ptype.PuntoCorner
RaiseEvent PointSelected(m_isel)
End If
Next
End If
If m_bVisPuntiCaliH Then
For i = 0 To 3
px = -m_Origin.X * m_ZoomFactor + m_pCaliH(i).X * m_ZoomFactor
py = -m_Origin.Y * m_ZoomFactor + m_pCaliH(i).Y * m_ZoomFactor
If System.Math.Abs(px - e.X) < std_radius And System.Math.Abs(py - e.Y) < std_radius Then
m_bPointSelected = 1
m_isel = i
m_jsel = 0
i = 3
m_ptypesel = ptype.PuntoCaliH
End If
Next
End If
If m_bVisAreaGen Then
For i = 0 To 3 Step 2
px = -m_Origin.X * m_ZoomFactor + m_pArea(i).X * m_ZoomFactor
py = -m_Origin.Y * m_ZoomFactor + m_pArea(i).Y * m_ZoomFactor
If System.Math.Abs(px - e.X) < dA_radius And System.Math.Abs(py - e.Y) < dA_radius Then
m_bPointSelected = 1
m_isel = i
m_jsel = 0
i = 3
m_ptypesel = ptype.PuntoArea
End If
Next
End If
' l'evento interessa solo per i punto corner
If (previusSelection <> 0 AndAlso m_bPointSelected = 0) OrElse
(previusPtypeSel = ptype.PuntoCorner AndAlso m_ptypesel <> ptype.PuntoCorner) Then
RaiseEvent PointUnselected()
End If
Me.Focus()
End Sub
Private Sub ImageViewer_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If m_OriginalImage Is Nothing Then Exit Sub
Dim Px As Single, Py As Single
Px = e.X
Py = e.Y
If Px > ClientSize.Width Then
Px = ClientSize.Width
End If
If Py > ClientSize.Height Then
Py = ClientSize.Height
End If
If e.X < 0 Then Px = 0
If e.Y < 0 Then Py = 0
If e.Button = m_PanButton OrElse e.Button = m_EditButton Then
Dim DeltaX As Integer, DeltaY As Integer
If PanMode Then
DeltaX = (m_StartPoint.X - Px) / m_ZoomFactor
DeltaY = (m_StartPoint.Y - Py) / m_ZoomFactor
'reset the startpoints
If (DeltaX <> 0) Then
m_StartPoint.X = m_StartPoint.X - DeltaX * m_ZoomFactor 'e.X
End If
If (DeltaY <> 0) Then
m_StartPoint.Y = m_StartPoint.Y - DeltaY * m_ZoomFactor 'e.Y
End If
Else
DeltaX = (m_StartPoint.X - Px)
DeltaY = (m_StartPoint.Y - Py)
End If
If m_bPointSelected And PanMode AndAlso e.Button = m_EditButton Then
If m_ptypesel = ptype.PuntoCorner Then
If m_bPointSelected = 2 Then
m_pCaliCornerH(m_isel).X = m_pCaliCornerH(m_isel).X - DeltaX
m_pCaliCornerH(m_isel).Y = m_pCaliCornerH(m_isel).Y - DeltaY
Else
m_pCaliCorner(m_isel).X = m_pCaliCorner(m_isel).X - DeltaX
m_pCaliCorner(m_isel).Y = m_pCaliCorner(m_isel).Y - DeltaY
' muovo entrambi
m_pCaliCornerH(m_isel).X = m_pCaliCornerH(m_isel).X - DeltaX
m_pCaliCornerH(m_isel).Y = m_pCaliCornerH(m_isel).Y - DeltaY
End If
'CheckPointBounds() ' Da sistemare!!!
ElseIf m_ptypesel = ptype.PuntoCaliH Then
m_pCaliH(m_isel).X = m_pCaliH(m_isel).X - DeltaX
m_pCaliH(m_isel).Y = m_pCaliH(m_isel).Y - DeltaY
ElseIf m_ptypesel = ptype.PuntoArea Then
If m_isel = 0 Then
For i As Integer = 0 To 3
m_pArea(i).X = m_pArea(i).X - DeltaX
m_pArea(i).Y = m_pArea(i).Y - DeltaY
Next
ElseIf m_isel = 2 Then
m_pArea(1).X = m_pArea(1).X - DeltaX
m_pArea(2).X = m_pArea(1).X
m_pArea(2).Y = m_pArea(2).Y - DeltaY
m_pArea(3).Y = m_pArea(2).Y
End If
End If
Me.Invalidate()
Exit Sub
End If
If PanMode And e.Button = m_PanButton Then
'Set the origin of the new image
m_Origin.X = m_Origin.X + DeltaX
m_Origin.Y = m_Origin.Y + DeltaY
CheckBounds()
'Force a paint
Me.Invalidate()
Else
Draw_Rectangle(e)
End If
End If
If (e.Button = MouseButtons.None) Then
' calcolo la posizione del mouse sull'immagine
Dim pimg As PointF
' inverso di:
'ix = -m_Origin.X * m_ZoomFactor + pimg.X * m_ZoomFactor
'iy = -m_Origin.Y * m_ZoomFactor + pimg.Y * m_ZoomFactor
'ix = (pimg.x-m_Origin.X)*m_ZoomFactor
'(pimg.x-m_Origin.X) = ix/m_ZoomFactor
pimg.X = m_Origin.X + e.X / m_ZoomFactor
pimg.Y = m_Origin.Y + e.Y / m_ZoomFactor
RaiseEvent NewMousePosImage(pimg)
End If
End Sub
' Metodo che dovrebbe verificare di che lo spostamento dell'immagine dentro i contorni
Private Sub CheckBounds()
If m_OriginalImage Is Nothing Then Exit Sub
' Il controllo che segue non mi soddisfa...!!!! NASCONDE SEMPRE IL LATO BASSO DELL'IMMAGINE !!!!
Return
'Make sure we don't go out of bounds
If m_Origin.X < 0 Then m_Origin.X = 0
If m_Origin.Y < 0 Then m_Origin.Y = 0
If m_Origin.X > m_OriginalImage.Width - (ClientSize.Width / m_ZoomFactor) Then
m_Origin.X = m_OriginalImage.Width - (ClientSize.Width / m_ZoomFactor)
End If
If m_Origin.Y > m_OriginalImage.Height - (ClientSize.Height / m_ZoomFactor) Then
m_Origin.Y = m_OriginalImage.Height - (ClientSize.Height / m_ZoomFactor)
End If
If m_Origin.X < 0 Then m_Origin.X = 0
If m_Origin.Y < 0 Then m_Origin.Y = 0
End Sub
' Metodo NON utilizzato
Private Sub CheckPointBounds()
If m_OriginalImage Is Nothing Then Exit Sub
'Make sure we don't go out of bounds
If m_pCaliCorner(m_isel).X < 0 Then m_pCaliCorner(m_isel).X = 0
If m_pCaliCorner(m_isel).Y < 0 Then m_pCaliCorner(m_isel).Y = 0
If m_pCaliCorner(m_isel).X > m_OriginalImage.Width - (ClientSize.Width / m_ZoomFactor) Then
m_pCaliCorner(m_isel).X = m_OriginalImage.Width - (ClientSize.Width / m_ZoomFactor)
End If
If m_pCaliCorner(m_isel).Y > m_OriginalImage.Height Then ' - (ClientSize.Height / m_ZoomFactor) Then
m_pCaliCorner(m_isel).Y = m_OriginalImage.Height '- (ClientSize.Height / m_ZoomFactor)
End If
' anche per i punti area
If m_pArea(m_isel).X < 0 Then m_pArea(m_isel).X = 0
If m_pArea(m_isel).Y < 0 Then m_pArea(m_isel).Y = 0
If m_pArea(m_isel).X > m_OriginalImage.Width - (ClientSize.Width / m_ZoomFactor) Then
m_pArea(m_isel).X = m_OriginalImage.Width - (ClientSize.Width / m_ZoomFactor)
End If
If m_pArea(m_isel).Y > m_OriginalImage.Height Then ' - (ClientSize.Height / m_ZoomFactor) Then
m_pArea(m_isel).Y = m_OriginalImage.Height '- (ClientSize.Height / m_ZoomFactor)
End If
End Sub
Private Sub DrawingBoard_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
If m_OriginalImage Is Nothing Then Exit Sub
If Not PanMode Then
EndPoint = New Point(e.X, e.Y)
If Selected_Rectangle = Nothing Then Exit Sub
ZoomSelection()
PanMode = True
End If
End Sub
Private Sub ZoomSelection()
If m_OriginalImage Is Nothing Then Exit Sub
Try
Dim NewOrigin As New Point(CInt(Me.Origin.X + (Selected_Rectangle.X / ZoomFactor)), _
Me.Origin.Y + (Selected_Rectangle.Y / ZoomFactor))
Dim NewFactor As Double
If Selected_Rectangle.Width > Selected_Rectangle.Height Then
NewFactor = (ClientSize.Width / (Selected_Rectangle.Width / ZoomFactor))
Else
NewFactor = (ClientSize.Height / (Selected_Rectangle.Height / ZoomFactor))
End If
Me.Origin = NewOrigin
Me.ZoomFactor = NewFactor
Catch ex As Exception
Throw ex
End Try
Selected_Rectangle = Nothing
End Sub
Private Sub ImageViewer_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
If Not ZoomOnMouseWheel Then Exit Sub
'set new zoomfactor
If e.Delta > 0 Then
ZoomImage(True, e.Location)
ElseIf e.Delta < 0 Then
ZoomImage(False, e.Location)
End If
End Sub
Public Sub RotateFlip(ByVal RotateFlipType As System.Drawing.RotateFlipType)
If m_OriginalImage Is Nothing Then Exit Sub
m_OriginalImage.RotateFlip(RotateFlipType)
Me.Invalidate()
End Sub
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
ReDim m_pCaliH(4)
ReDim m_pCaliCorner(4)
ReDim m_pCaliCornerH(4)
End Sub
Private Sub DrawingBoard_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
Me.ComputeDrawingArea()
If Me.StretchImageToFit Then Me.Invalidate()
End Sub
Private Sub DrawingBoard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Sub SetPuntoCorner(ByVal i As Integer, ByVal x As Double, ByVal y As Double)
If i >= 0 And i < 4 Then
m_pCaliCorner(i).X = x
m_pCaliCorner(i).Y = y
End If
End Sub
Public Sub SetPuntoCornerH(ByVal i As Integer, ByVal x As Double, ByVal y As Double)
If i >= 0 And i < 4 Then
m_pCaliCornerH(i).X = x
m_pCaliCornerH(i).Y = y
End If
End Sub
Public Sub SetPuntoH(ByVal i As Integer, ByVal x As Double, ByVal y As Double)
If i >= 0 And i < 4 Then
m_pCaliH(i).X = x
m_pCaliH(i).Y = y
End If
End Sub
Public Function GetXCorner(ByVal i As Integer) As Integer
GetXCorner = m_pCaliCorner(i).X
End Function
Public Function GetYCorner(ByVal i As Integer) As Integer
GetYCorner = m_pCaliCorner(i).Y
End Function
Public Function GetXCornerH(ByVal i As Integer) As Integer
GetXCornerH = m_pCaliCornerH(i).X
End Function
Public Function GetYCornerH(ByVal i As Integer) As Integer
GetYCornerH = m_pCaliCornerH(i).Y
End Function
Public Function GetXHpx(ByVal i As Integer) As Integer
GetXHpx = m_pCaliH(i).X
End Function
Public Function GetYHpx(ByVal i As Integer) As Integer
GetYHpx = m_pCaliH(i).Y
End Function
Public Sub SetPuntoArea(ByVal i As Integer, ByVal x As Double, ByVal y As Double)
If i >= 0 And i < 4 Then
m_pArea(i).X = x
m_pArea(i).Y = y
End If
End Sub
Public Sub SetArea(x1 As Double, y1 As Double, x2 As Double, y2 As Double)
m_pArea(0).X = x1
m_pArea(0).Y = y1
m_pArea(1).X = x2
m_pArea(1).Y = y1
m_pArea(2).X = x2
m_pArea(2).Y = y2
m_pArea(3).X = x1
m_pArea(3).Y = y2
End Sub
Public Function GetXPArea(ByVal i As Integer) As Integer
Return m_pArea(i).X
End Function
Public Function GetYPArea(ByVal i As Integer) As Integer
Return m_pArea(i).Y
End Function
Public Sub GetArea(ByRef x1 As Double, ByRef y1 As Double, ByRef x2 As Double, ByRef y2 As Double)
x1 = m_pArea(0).X
y1 = m_pArea(0).Y
x2 = m_pArea(2).X
y2 = m_pArea(2).Y
End Sub
End Class