试图创建一个四角拖动图像sizer控件

时间:2015-04-06 15:03:40

标签: vb.net image winforms resize picturebox

我即将创建一个应用程序,将许多图像放到画布区域。我想把图像放在那里并用鼠标拖动它们。

但是我很难在图像周围实现特定的框架,我可以通过点击四个角来调整它们的大小。

这是基本代码,仅用于放置图像并移动它们。现在我想要获得每个图像周围四个角框架调整图像大小的功能。任何想法都非常感激。

Imports System.IO
Imports System.Drawing.Printing
Imports CtlResize

Public Class Form1
  Dim WithEvents pbxNewPicturebox As New PictureBox
  Dim ArrayOfPicturebox(10) As PictureBox
  Dim index As Integer

  Private Sub DynamicMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim pbxPicturebox As Picturebox = DirectCast(sender, Picturebox)
    Static mousePos As Point

    If e.Button = Windows.Forms.MouseButtons.None Then
      mousePos.X = e.X
      mousePos.Y = e.Y
    Else
      pbxPicturebox.Left = pbxPicturebox.Left + (e.X - mousePos.X)
      pbxPicturebox.Top = pbxPicturebox.Top + (e.Y - mousePos.Y)
    End If
  End Sub

  Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
    OpenFileDialog1.ShowDialog()
    If OpenFileDialog1.FileName = "" Then Exit Sub
    ArrayOfPicturebox(index) = New Picturebox
    Controls.Add(ArrayOfPicturebox(index))
    ArrayOfPicturebox(index).Parent = PictureBox1
    ArrayOfPicturebox(index).Image = Image.FromFile(OpenFileDialog1.FileName)
    ArrayOfPicturebox(index).Visible = True
    ArrayOfPicturebox(index).SizeMode = PictureBoxSizeMode.AutoSize
    ArrayOfPicturebox(index).Refresh()
    ArrayOfPicturebox(index).Name = CStr(index)

    AddHandler ArrayOfPicturebox(index).MouseMove, AddressOf DynamicMouseMove
    index = index + 1
  End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,可以在这里提出解决方案:

Imports System.ComponentModel


Public Class PictureBoxEx

Inherits PictureBox

Private Const rectsize = 10

Private mMousePointDown As Point
Private mMousePointDownScrCo As Point
Private mMousePointMove As Point
Private mMousePointUp As Point

Private sMousePointDeltaX As Single
Private iPicBoxTopWhenMouseDown As Integer = 0
Private iPicBoxLeftWhenMouseDown As Integer = 0
Private iPicBoxTopWhenMouseDownSC As Integer = 0
Private iPicBoxLeftWhenMouseDownSC As Integer = 0
Private iPicBoxOldHeight As Integer
Private iPicBoxOldWidth As Integer

Private bDrawRect As Boolean = False

Private bPicBoxMouseDownFlag As Boolean = False
Private bPicBoxMouseMovedFlag As Boolean = False
Private bMouseIsOnRectTopLeftFlag As Boolean = False
Private bMouseIsOnRectTopFlag As Boolean = False
Private bMouseIsOnRectTopRightFlag As Boolean = False
Private bMouseIsOnRectRightFlag As Boolean = False
Private bMouseIsOnRectBottomRightFlag As Boolean = False
Private bMouseIsOnRectBottomFlag As Boolean = False
Private bMouseIsOnRectBottomLeftFlag As Boolean = False
Private bMouseIsOnRectLeftFlag As Boolean = False
Private bResizingPicBoxFlag As Boolean = False
Private bCheckDragFlag As Boolean = False
Private bDragOKFlag As Boolean = False

Private Sub PictureBoxEx_MouseClick(sender As Object, e As EventArgs) Handles Me.Click

    Dim pb As PictureBox = DirectCast(sender, PictureBox)

    ' switch selection rectangle on and off
    If bDrawRect = False Then
        bDrawRect = True
        pb.Invalidate()
    Else
        bDrawRect = False
        pb.Invalidate()
    End If

End Sub

Private Sub PictureBoxEx_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown

    Dim pb As PictureBox = DirectCast(sender, PictureBox)

    ' get top and left border of picture box in client coordinates
    pb = CType(sender, PictureBox)
    iPicBoxTopWhenMouseDown = pb.Top
    iPicBoxLeftWhenMouseDown = pb.Left

    ' transform to client coordinates
    Dim myPoint As New Point(pb.Left, pb.Top)
    iPicBoxTopWhenMouseDownSC = PointToScreen(myPoint).Y
    iPicBoxLeftWhenMouseDownSC = PointToScreen(myPoint).X

    ' set flag that mouse is down on picture box
    bPicBoxMouseDownFlag = True

    ' Reset flag for mouse starts dragging. This flag is set to true in 
    ' Sub PictureBox1_MouseMove, when mouse leaves a little rectangle area 
    ' around mouse down point.
    bDragOKFlag = False

    ' get client coordinates of mouse point
    mMousePointDown = e.Location

    ' compute screen coordinates of mouse point
    mMousePointDownScrCo = sender.PointToScreen(mMousePointDown)

    ' set picture box to top of z-order
    pb.BringToFront()

    ' store height of picture box
    iPicBoxOldHeight = sender.Height

    ' store width of picture box
    iPicBoxOldWidth = sender.Width

    ' set flag for "mouse is on little rectangle at top border of picture box"
    If MouseIsOnRectTopLeft(sender, mMousePointDown) Then
        bMouseIsOnRectTopLeftFlag = True
    ElseIf MouseIsOnRectTop(sender, mMousePointDown) Then
        bMouseIsOnRectTopFlag = True
    ElseIf MouseIsOnRectTopRight(sender, mMousePointDown) Then
        bMouseIsOnRectTopRightFlag = True
    ElseIf MouseIsOnRectRight(sender, mMousePointDown) Then
        bMouseIsOnRectRightFlag = True
    ElseIf MouseIsOnRectBottomRight(sender, mMousePointDown) Then
        bMouseIsOnRectBottomRightFlag = True
    ElseIf MouseIsOnRectBottom(sender, mMousePointDown) Then
        bMouseIsOnRectBottomFlag = True
    ElseIf MouseIsOnRectBottomLeft(sender, mMousePointDown) Then
        bMouseIsOnRectBottomLeftFlag = True
    ElseIf MouseIsOnRectLeft(sender, mMousePointDown) Then
        bMouseIsOnRectLeftFlag = True
    Else
        ' set drag cursor when picture box is selected
        Me.Cursor = Cursors.Hand

        ' set flag that drag check (mouse in/out little rectangle
        ' around mouse down point?) has started
        bCheckDragFlag = True
    End If

End Sub

Private Sub PictureBoxEx_MouseLeave(sender As Object, e As EventArgs) Handles Me.MouseLeave

    Dim pb As PictureBox = DirectCast(sender, PictureBox)

    ' reset cursor when mouse leaves picture box
    Me.Cursor = Cursors.Arrow

    UndrawSelRectAndSetFlags(pb)

End Sub

Private Sub PictureBoxEx_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove

    Dim pb As PictureBox = DirectCast(sender, PictureBox)

    Dim sMousePointDeltaX As Single
    Dim sMousePointDeltaY As Single
    Dim sPicBoxOldX As Single
    Dim sPicBoxOldY As Single

    Dim sPointOnScreen As Point

    Dim sMyParent As Control
    Dim sMyParentClientHeight As Integer
    Dim sMyParentClientWidth As Integer

    ' dimensions of parent control do not change while this block is running
    sMyParent = pb.Parent
    sMyParentClientHeight = sMyParent.ClientRectangle.Height
    sMyParentClientWidth = sMyParent.ClientRectangle.Width


    ' get location of mouse cursor in client coordinates
    mMousePointMove = e.Location

    ' set DragOKFlag to True when mouse leaves little rectangle around
    ' mouse down point
    If ((mMousePointMove.X < (mMousePointDown.X - 3)) Or (mMousePointMove.X > (mMousePointDown.X + 3)) Or (mMousePointMove.Y < (mMousePointDown.Y - 3)) Or (mMousePointMove.Y > (mMousePointDown.Y + 3))) And bPicBoxMouseDownFlag = True Then
        bDragOKFlag = True
    End If

    ' compute point with screen coordinates
    sPointOnScreen = sender.PointToScreen(mMousePointMove)

    ' Perform this block when mouse cursor is in the little rectangle on
    ' top of the picture box or when mMouseIsOnRectTopFlag is set to True.
    ' mMouseIsOnRectTopFlag = True means that the picture box will continue
    ' to resize when mouse cursor leaves the little rectangle.
    If ((MouseIsOnRectTopLeft(sender, mMousePointMove) And Not bPicBoxMouseDownFlag) Or bMouseIsOnRectTopLeftFlag) And bDrawRect And Not bCheckDragFlag Then

        Me.Cursor = Cursors.SizeNWSE

        If bPicBoxMouseDownFlag Then

            UndrawSelRectAndSetFlags(pb)

            ' compute shift
            sMousePointDeltaX = sPointOnScreen.X - mMousePointDownScrCo.X
            sMousePointDeltaY = sPointOnScreen.Y - mMousePointDownScrCo.Y

            ' compute new height and new top of the picture box
            ' make sure, that the picture box does not get to small
            If (iPicBoxOldHeight - sMousePointDeltaY) < 10 Then
                pb.Height = 10
                pb.Top = iPicBoxTopWhenMouseDown + iPicBoxOldHeight - 10
                bDrawRect = True
            Else
                ' Attention: "PictureBox1.Top = ..." makes a redraw of the picture box! Do not use it in If-statement!!
                If iPicBoxTopWhenMouseDown + sMousePointDeltaY < 0 Then
                    ' now mouse cursor has left the parent control (Form1)
                    pb.Top = 0
                    ' add the old height to point of top border when mouse down event was entered
                    pb.Height = iPicBoxTopWhenMouseDown + iPicBoxOldHeight
                Else
                    pb.Height = iPicBoxOldHeight - sMousePointDeltaY
                    pb.Top = iPicBoxTopWhenMouseDown + sMousePointDeltaY
                End If
            End If

            If (iPicBoxOldWidth - sMousePointDeltaX) < 10 Then
                pb.Width = 10
                bDrawRect = True
            Else
                ' Attention: "PictureBox1.Top = ..." makes a redraw of the picture box! Do not use it in If-statement!!
                If iPicBoxLeftWhenMouseDown + sMousePointDeltaX < 0 Then
                    ' now mouse cursor has left the parent control (Form1)
                    pb.Left = 0
                    ' add the old width to point of left border when mouse down event was entered
                    pb.Width = iPicBoxLeftWhenMouseDown + iPicBoxOldWidth
                Else
                    pb.Width = iPicBoxOldWidth - sMousePointDeltaX
                    pb.Left = iPicBoxLeftWhenMouseDown + sMousePointDeltaX
                End If
            End If

        End If

    ElseIf ((MouseIsOnRectTop(sender, mMousePointMove) And Not bPicBoxMouseDownFlag) Or bMouseIsOnRectTopFlag) And bDrawRect And Not bCheckDragFlag Then

        Me.Cursor = Cursors.SizeNS

        If bPicBoxMouseDownFlag = True Then

            UndrawSelRectAndSetFlags(pb)

            ' compute shift
            sMousePointDeltaY = sPointOnScreen.Y - mMousePointDownScrCo.Y

            ' compute new height and new top of the picture box make sure, that the picture box does not get to small
            If (iPicBoxOldHeight - sMousePointDeltaY) < 10 Then
                pb.Height = 10
                pb.Top = iPicBoxTopWhenMouseDown + iPicBoxOldHeight - 10
                bDrawRect = True
            Else
                If (iPicBoxTopWhenMouseDown + sMousePointDeltaY) < 0 Then
                    pb.Height = iPicBoxTopWhenMouseDown + iPicBoxOldHeight
                    pb.Top = 0
                Else
                    pb.Height = iPicBoxOldHeight - sMousePointDeltaY
                    pb.Top = iPicBoxTopWhenMouseDown + sMousePointDeltaY
                End If
            End If

        End If

    ElseIf ((MouseIsOnRectTopRight(sender, mMousePointMove) And Not bPicBoxMouseDownFlag) Or bMouseIsOnRectTopRightFlag) And bDrawRect And Not bCheckDragFlag Then

        Me.Cursor = Cursors.SizeNESW

        If bPicBoxMouseDownFlag = True Then

            UndrawSelRectAndSetFlags(pb)

            ' compute shift
            sMousePointDeltaX = sPointOnScreen.X - mMousePointDownScrCo.X
            sMousePointDeltaY = sPointOnScreen.Y - mMousePointDownScrCo.Y

            ' compute new height and width of the picture box. make sure, that the picture box does not get to small
            If (iPicBoxOldHeight - sMousePointDeltaY) < 10 Then
                pb.Height = 10
                pb.Top = iPicBoxTopWhenMouseDown + iPicBoxOldHeight - 10
                bDrawRect = True
            Else
                If (iPicBoxTopWhenMouseDown + sMousePointDeltaY) < 0 Then
                    pb.Height = iPicBoxTopWhenMouseDown + iPicBoxOldHeight
                    pb.Top = 0
                Else
                    pb.Height = iPicBoxOldHeight - sMousePointDeltaY
                    pb.Top = iPicBoxTopWhenMouseDown + sMousePointDeltaY
                End If
            End If

            If (iPicBoxOldWidth + sMousePointDeltaX) < 10 Then
                pb.Width = 10
                bDrawRect = True
            Else
                If (pb.Left + iPicBoxOldWidth + sMousePointDeltaX) > _
                sMyParentClientWidth Then
                    pb.Width = sMyParentClientWidth - iPicBoxLeftWhenMouseDown
                Else
                    pb.Width = iPicBoxOldWidth + sMousePointDeltaX
                End If
            End If

        End If

    ElseIf ((MouseIsOnRectRight(sender, mMousePointMove) And Not bPicBoxMouseDownFlag) Or bMouseIsOnRectRightFlag) And bDrawRect And Not bCheckDragFlag Then

        Me.Cursor = Cursors.SizeWE

        If bPicBoxMouseDownFlag = True Then

            UndrawSelRectAndSetFlags(pb)

            ' compute shift
            sMousePointDeltaX = sPointOnScreen.X - mMousePointDownScrCo.X

            ' compute new width of the picture box. make sure, that the picture box does not get to small
            If (iPicBoxOldWidth + sMousePointDeltaX) < 10 Then
                pb.Width = 10
                bDrawRect = True
            Else
                If (pb.Left + iPicBoxOldWidth + sMousePointDeltaX) > sMyParentClientWidth Then
                    pb.Width = sMyParentClientWidth - iPicBoxLeftWhenMouseDown
                Else
                    pb.Width = iPicBoxOldWidth + sMousePointDeltaX
                End If
            End If
        End If

    ElseIf ((MouseIsOnRectBottomRight(sender, mMousePointMove) And Not bPicBoxMouseDownFlag) Or bMouseIsOnRectBottomRightFlag) And bDrawRect And Not bCheckDragFlag Then

        Me.Cursor = Cursors.SizeNWSE

        If bPicBoxMouseDownFlag = True Then

            UndrawSelRectAndSetFlags(pb)

            ' compute shift
            sMousePointDeltaX = sPointOnScreen.X - mMousePointDownScrCo.X
            sMousePointDeltaY = sPointOnScreen.Y - mMousePointDownScrCo.Y

            ' compute new height and width of the picture box. make sure, that the picture box does not get to small
            If (iPicBoxOldWidth + sMousePointDeltaX) < 10 Then
                pb.Width = 10
                bDrawRect = True
            Else
                'PictureBox1.Width = mPicBoxOldWidth + sMousePointDeltaX
                If (pb.Left + iPicBoxOldWidth + sMousePointDeltaX) > _
                sMyParentClientWidth Then
                    pb.Width = sMyParentClientWidth - iPicBoxLeftWhenMouseDown
                Else
                    pb.Width = iPicBoxOldWidth + sMousePointDeltaX
                End If
            End If

            ' picture box top does not change
            If (iPicBoxOldHeight + sMousePointDeltaY) < 10 Then
                pb.Height = 10
                bDrawRect = True
            Else
                If (pb.Top + iPicBoxOldHeight + sMousePointDeltaY) > sMyParentClientHeight Then
                    pb.Height = sMyParentClientHeight - iPicBoxTopWhenMouseDown
                Else
                    pb.Height = iPicBoxOldHeight + sMousePointDeltaY
                End If
            End If

        End If

    ElseIf ((MouseIsOnRectBottom(sender, mMousePointMove) And Not bPicBoxMouseDownFlag) Or bMouseIsOnRectBottomFlag) And bDrawRect And Not bCheckDragFlag Then

        Me.Cursor = Cursors.SizeNS

        If bPicBoxMouseDownFlag = True Then

            UndrawSelRectAndSetFlags(pb)

            ' compute shift
            sMousePointDeltaY = sPointOnScreen.Y - mMousePointDownScrCo.Y

            ' compute new height of the picture box. make sure, that the picture box does not get to small. picture box top does not change
            If (iPicBoxOldHeight + sMousePointDeltaY) < 10 Then
                pb.Height = 10
                bDrawRect = True
            Else
                If (pb.Top + iPicBoxOldHeight + sMousePointDeltaY) > sMyParentClientHeight Then
                    pb.Height = sMyParentClientHeight - iPicBoxTopWhenMouseDown
                Else
                    pb.Height = iPicBoxOldHeight + sMousePointDeltaY
                End If
            End If

        End If

    ElseIf ((MouseIsOnRectBottomLeft(sender, mMousePointMove) And Not bPicBoxMouseDownFlag) Or bMouseIsOnRectBottomLeftFlag) And bDrawRect And Not bCheckDragFlag Then

        Me.Cursor = Cursors.SizeNESW


        If bPicBoxMouseDownFlag = True Then

            UndrawSelRectAndSetFlags(pb)

            ' compute shift
            sMousePointDeltaX = sPointOnScreen.X - mMousePointDownScrCo.X
            sMousePointDeltaY = sPointOnScreen.Y - mMousePointDownScrCo.Y

            ' compute new height and width of the picture box. make sure, that the picture box does not get to small. picture box top does not change
            If (iPicBoxOldHeight + sMousePointDeltaY) < 10 Then
                pb.Height = 10
                bDrawRect = True
            Else
                If (pb.Top + iPicBoxOldHeight + sMousePointDeltaY) > sMyParentClientHeight Then
                    pb.Height = sMyParentClientHeight - iPicBoxTopWhenMouseDown
                Else
                    pb.Height = iPicBoxOldHeight + sMousePointDeltaY
                End If
            End If

            If (iPicBoxOldWidth - sMousePointDeltaX) < 10 Then
                pb.Width = 10
                bDrawRect = True
            Else
                If iPicBoxLeftWhenMouseDown + sMousePointDeltaX < 0 Then
                    ' now mouse cursor has left the parent control (Form1)
                    pb.Left = 0
                    ' add the old width to point of left border when mouse down event was entered
                    pb.Width = iPicBoxLeftWhenMouseDown + iPicBoxOldWidth
                Else
                    pb.Width = iPicBoxOldWidth - sMousePointDeltaX
                    pb.Left = iPicBoxLeftWhenMouseDown + sMousePointDeltaX
                End If
            End If

        End If

    ElseIf ((MouseIsOnRectLeft(sender, mMousePointMove) And Not bPicBoxMouseDownFlag) Or bMouseIsOnRectLeftFlag) And bDrawRect And Not bCheckDragFlag Then

        Me.Cursor = Cursors.SizeWE

        If bPicBoxMouseDownFlag Then

            UndrawSelRectAndSetFlags(pb)

            ' compute shift
            sMousePointDeltaX = sPointOnScreen.X - mMousePointDownScrCo.X

            ' compute new width of the picture box. make sure, that the picture box does not get to small. picture box top does not change
            If (iPicBoxOldWidth - sMousePointDeltaX) < 10 Then
                pb.Width = 10
                bDrawRect = True
            Else
                If iPicBoxLeftWhenMouseDown + sMousePointDeltaX < 0 Then
                    ' now mouse cursor has left the parent control (Form1)
                    pb.Left = 0
                    ' add the old width to point of left border when mouse down event
                    ' was entered
                    pb.Width = iPicBoxLeftWhenMouseDown + iPicBoxOldWidth
                Else
                    pb.Width = iPicBoxOldWidth - sMousePointDeltaX
                    pb.Left = iPicBoxLeftWhenMouseDown + sMousePointDeltaX
                End If
            End If

        End If

    ElseIf Not bPicBoxMouseDownFlag Then

        Me.Cursor = Cursors.Arrow

    ElseIf bDragOKFlag Then

        ' "mblnDragOKFlag = True" means drag only when mouse leaves. little rectangle around mouse down point

        If bPicBoxMouseDownFlag And Not bMouseIsOnRectTopFlag And Not bMouseIsOnRectRightFlag And Not bMouseIsOnRectTopRightFlag Then

            ' set flag that cursor was moved so that selection rectangle
            ' will stay after releasing mouse button
            bPicBoxMouseMovedFlag = True

            'uncommented code
            If bDrawRect Then
                bDrawRect = False
                pb.Invalidate()
            End If

            ' should I better use screen coordinates here?
            sMousePointDeltaX = mMousePointMove.X - mMousePointDown.X
            sMousePointDeltaY = mMousePointMove.Y - mMousePointDown.Y

            ' store old position
            sPicBoxOldX = pb.Location.X
            sPicBoxOldY = pb.Location.Y

            ' compute new position. make sure that picture box does not leave the form
            If sPicBoxOldX + sMousePointDeltaX < 0 Then
                pb.Left = 0
            ElseIf sPicBoxOldX + pb.Width + sMousePointDeltaX > sMyParentClientWidth Then
                pb.Left = sMyParentClientWidth - pb.Width
            Else
                pb.Left = sPicBoxOldX + sMousePointDeltaX
            End If

            If sPicBoxOldY + sMousePointDeltaY < 0 Then
                pb.Top = 0
            ElseIf sPicBoxOldY + pb.Height + sMousePointDeltaY > sMyParentClientHeight Then
                pb.Top = sMyParentClientHeight - pb.Height
            Else
                pb.Top = sPicBoxOldY + sMousePointDeltaY
            End If

        End If

    End If

End Sub

Private Sub UndrawSelRectAndSetFlags(pb As PictureBox)

    ' on first entry in this block after pressing left mouse button
    ' undraw the selection rectangle
    If Not bResizingPicBoxFlag Then
        pb.Invalidate()
    End If

    ' set flag that picture box is on resizing to avoid dragging
    ' of the picture box
    bResizingPicBoxFlag = True


    ' set flag that cursor was moved
    bPicBoxMouseMovedFlag = True

End Sub

Private Function MouseIsOnRectTopLeft(ByVal sender As Object, ByVal MousePoint As Point) As Boolean

    MouseIsOnRectTopLeft = False

    If MousePoint.X >= 0 And MousePoint.X <= rectsize And MousePoint.Y >= 0 And MousePoint.Y <= rectsize Then
        MouseIsOnRectTopLeft = True
    End If

End Function

Private Function MouseIsOnRectTop(ByVal sender As Object, ByVal MousePoint As Point) As Boolean

    MouseIsOnRectTop = False

    If MousePoint.X >= (sender.Width / 2 - rectsize / 2) And MousePoint.X <= (sender.Width / 2 + rectsize / 2) And MousePoint.Y >= 0 And MousePoint.Y <= rectsize Then
        MouseIsOnRectTop = True
    End If

End Function

Private Function MouseIsOnRectTopRight(ByVal sender As Object, ByVal MousePoint As Point) As Boolean

    MouseIsOnRectTopRight = False

    If MousePoint.X >= (sender.Width - rectsize) And MousePoint.X <= sender.Width And MousePoint.Y >= 0 And MousePoint.Y <= rectsize Then
        MouseIsOnRectTopRight = True
    End If

End Function

Private Function MouseIsOnRectRight(ByVal sender As Object, ByVal MousePoint As Point) As Boolean

    MouseIsOnRectRight = False

    If MousePoint.X >= (sender.Width - rectsize) And MousePoint.X <= sender.Width And MousePoint.Y >= (sender.Height / 2 - rectsize / 2) And MousePoint.Y <= (sender.Height / 2 + rectsize / 2) Then
        MouseIsOnRectRight = True
    End If

End Function

Private Function MouseIsOnRectBottomRight(ByVal sender As Object, ByVal MousePoint As Point) As Boolean

    MouseIsOnRectBottomRight = False

    If MousePoint.X >= (sender.Width - rectsize) And MousePoint.X <= sender.Width And MousePoint.Y >= (sender.Height - rectsize) And MousePoint.Y <= sender.Height Then
        MouseIsOnRectBottomRight = True
    End If

End Function

Private Function MouseIsOnRectBottom(ByVal sender As Object, ByVal MousePoint As Point) As Boolean

    MouseIsOnRectBottom = False

    If MousePoint.X >= (sender.Width / 2 - rectsize / 2) And MousePoint.X <= (sender.Width / 2 + rectsize / 2) And MousePoint.Y >= (sender.Height - rectsize) And MousePoint.Y <= sender.Height Then
        MouseIsOnRectBottom = True
    End If

End Function

Private Function MouseIsOnRectBottomLeft(ByVal sender As Object, ByVal MousePoint As Point) As Boolean

    MouseIsOnRectBottomLeft = False

    If MousePoint.X >= 0 And MousePoint.X <= rectsize And MousePoint.Y >= (sender.Height - rectsize) And MousePoint.Y <= sender.Height Then
        MouseIsOnRectBottomLeft = True
    End If

End Function

Private Function MouseIsOnRectLeft(ByVal sender As Object, ByVal MousePoint As Point) As Boolean

    MouseIsOnRectLeft = False

    If MousePoint.X >= 0 And MousePoint.X <= rectsize And MousePoint.Y >= (sender.Height / 2 - rectsize / 2) And MousePoint.Y <= (sender.Height / 2 + rectsize / 2) Then
        MouseIsOnRectLeft = True
    End If

End Function

Private Sub PictureBoxEx_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp

    Dim pb As PictureBox = DirectCast(sender, PictureBox)

    ' reset flags after releasing mouse button

    bMouseIsOnRectTopLeftFlag = False
    bMouseIsOnRectTopFlag = False
    bMouseIsOnRectTopRightFlag = False
    bMouseIsOnRectRightFlag = False
    bMouseIsOnRectBottomRightFlag = False
    bMouseIsOnRectBottomFlag = False
    bMouseIsOnRectBottomLeftFlag = False
    bMouseIsOnRectLeftFlag = False

    If bResizingPicBoxFlag = True Then bDrawRect = True ' now rectangle will 
    ' be drawn again when it was drawn before dragging starts
    bResizingPicBoxFlag = False
    bCheckDragFlag = False
    If bDragOKFlag = True Then bDrawRect = True ' now rectangle will 
    ' be drawn again when it was drawn before dragging starts
    bDragOKFlag = False

    ' reset cursor from drag to normal
    Me.Cursor = Cursors.Arrow

    mMousePointUp = e.Location

    If bPicBoxMouseMovedFlag = True Then
        bDrawRect = True
        pb.Invalidate()
    End If

    ' reset flag
    bPicBoxMouseMovedFlag = False

    ' reset flag
    bPicBoxMouseDownFlag = False

End Sub

Private Sub PictureBoxEx_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint

    ' Create pen.
    Dim blackPen1 As New Pen(Color.Black, 1)
    Dim blackPen2 As New Pen(Color.Black, 2)

    ' Create rectangle.
    Dim rect As New Rectangle(0, 0, sender.Width, sender.Height)

    Dim sSenderWidth As Single = sender.Width / 2
    Dim sSenderHeight As Single = sender.Height / 2

    Dim rectTopLeft As New Rectangle(0, 0, rectsize, rectsize)
    Dim rectTop As New Rectangle(sSenderWidth - rectsize / 2, 0, rectsize, rectsize)
    Dim rectTopRight As New Rectangle(sender.Width - rectsize, 0, rectsize, rectsize)
    Dim rectRight As New Rectangle(sender.Width - rectsize, sSenderHeight - rectsize / 2, rectsize, rectsize)
    Dim rectBottomRight As New Rectangle(sSenderWidth - rectsize / 2, sender.Height - rectsize, rectsize, rectsize)
    Dim rectBottom As New Rectangle(sender.Width - rectsize, sender.Height - rectsize, rectsize, rectsize)
    Dim rectBottomLeft As New Rectangle(0, sender.Height - rectsize, rectsize, rectsize)
    Dim rectLeft As New Rectangle(0, sSenderHeight - rectsize / 2, rectsize, rectsize)


    ' If mDrawRect = True And mblnResizingPicBoxFlag = False Then
    If (bDrawRect And Not bResizingPicBoxFlag) And (bDrawRect And Not bDragOKFlag) Then

        ' Draw rectangle to screen.
        e.Graphics.DrawRectangle(blackPen2, rect)
        e.Graphics.DrawRectangle(blackPen1, rectTopLeft)
        e.Graphics.DrawRectangle(blackPen1, rectTop)
        e.Graphics.DrawRectangle(blackPen1, rectTopRight)
        e.Graphics.DrawRectangle(blackPen1, rectRight)
        e.Graphics.DrawRectangle(blackPen1, rectBottomRight)
        e.Graphics.DrawRectangle(blackPen1, rectBottom)
        e.Graphics.DrawRectangle(blackPen1, rectBottomLeft)
        e.Graphics.DrawRectangle(blackPen1, rectLeft)

    End If

End Sub

End Class