'Events to select a portion of image using mouse
Private Sub pictureBox1_MouseClick(sender As Object, e As MouseEventArgs) _
Handles pictureBox1.MouseClick
If (e.Button And MouseButtons.Left) <> 0 Then
Dim dcs = SystemInformation.DoubleClickSize
If Math.Abs(e.X - start.X) _
< dcs.Width AndAlso Math.Abs(e.Y - start.Y) < dcs.Height Then
selection = New RectF(1.0F, 1.0F)
pictureBox1.Invalidate()
End If
End If
End Sub
Private Sub pictureBox1_MouseDown(sender As Object, e As MouseEventArgs) _
Handles pictureBox1.MouseDown
If (e.Button And MouseButtons.Left) <> 0 Then
start = New Point2L(e.X, e.Y)
End If
End Sub
Private Sub pictureBox1_MouseMove(sender As Object, e As MouseEventArgs) _
Handles pictureBox1.MouseMove
If (e.Button And MouseButtons.Left) <> 0 Then
Dim w As Integer = pictureBox1.Width
Dim h As Integer = pictureBox1.Height
Dim x As Integer = Math.Max(0, Math.Min(e.X, w))
Dim y As Integer = Math.Max(0, Math.Min(e.Y, h))
selection = New RectF(CSng(Math.Min(start.X, x)) / w,
CSng(Math.Min(start.Y, y)) / h,
CSng(Math.Abs(x - start.X)) / w,
CSng(Math.Abs(y - start.Y)) / h)
pictureBox1.Invalidate()
End If
End Sub
Private Sub pictureBox1_Paint(sender As Object, e As PaintEventArgs) _
Handles pictureBox1.Paint
Dim w As Integer = pictureBox1.Width
Dim h As Integer = pictureBox1.Height
Dim path = New GraphicsPath(FillMode.Alternate)
path.AddRectangle(New RectangleF(0, 0, w, h))
path.AddRectangle(New RectangleF(selection.X * w,
selection.Y * h,
selection.Width * w,
selection.Height * h))
Dim brush = New SolidBrush(Color.FromArgb(&H66FFFFFF))
e.Graphics.FillPath(brush, path)
brush.Dispose()
path.Dispose()
End Sub