Private Sub MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles ctl.MouseMove
If Drawing And Not Drawn Then
Dim x As Integer = e.X
Dim y As Integer = e.Y
'bisheriges Rechteck löschen
Remove()
'Größe des neuen Rechtecks ermitteln
If x < 0 Then x = 0
If x > ctl.ClientSize.Width Then x = ctl.ClientSize.Width
If y < 0 Then y = 0
If y > ctl.ClientSize.Height Then y = ctl.ClientSize.Height
'neues Rechteck zeichnen
Size = New Size(x - OriginalPosition.X, y - OriginalPosition.Y)
Draw()
End If
End Sub
Private Sub MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles ctl.MouseDown
If Drawn Then
Remove()
Else
If Drawing Then Return
OriginalPosition = New Point(e.X, e.Y)
Position = ctl.PointToScreen(OriginalPosition)
Size = New Size(0, 0)
'Startrechteck zeichnen
Draw()
End If
End Sub
Private Sub MouseUp(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles ctl.MouseUp
Dim tmp As Integer
If Drawing Then
x0 = OriginalPosition.X
x1 = x0 + Size.Width
If x0 > x1 Then tmp = x0 : x0 = x1 : x1 = tmp
y0 = OriginalPosition.Y
y1 = y0 + Size.Height
If y0 > y1 Then tmp = y0 : y0 = y1 : y1 = tmp
Drawing = False
Drawn = True
If x1 - x0 = 0 Or y1 - y0 = 0 Then
Remove()
Else
OnSelected(x0, y0, x1 - x0, y1 - y0)
End If
Return
End If
End Sub
Private Sub Leave(ByVal sender As Object, ByVal e As EventArgs) Handles _
ctl.MouseLeave
Drawn = Drawn And True
Drawing = False
Remove()
End Sub
Protected Overridable Sub OnSelected(ByVal x As Integer, ByVal y As _
Integer, ByVal width As Integer, ByVal height As Integer)
RaiseEvent Selected(Me, New RubberBoxEventArgs(ctl, x, y, width, _
height))
End Sub
<Description("Get the selected image portion from a supplied Image"), _
EditorBrowsable(EditorBrowsableState.Always)> _
Public Overridable Function CutSelection(ByVal img As Image, ByVal _
isStretched As Boolean) As Image
Dim gn As Graphics
Dim go As Graphics
Dim width As Integer = x1 - x0
Dim height As Integer = y1 - y0
Dim newImage As Bitmap
If Not Drawn Then Return Nothing
If isStretched Then
Dim Ratio_Width As Double = img.Width / ctl.Width
Dim Ratio_hight As Double = img.Height / ctl.Height
x0 *= Ratio_Width
x1 *= Ratio_Width
y0 *= Ratio_hight
y1 *= Ratio_hight
End If
Try
go = ctl.CreateGraphics()
newImage = New Bitmap(width, height, go)
gn = Graphics.FromImage(newImage)
gn.DrawImage(img, New Rectangle(0, 0, width, height), New Rectangle( _
x0, y0, width, height), GraphicsUnit.Pixel)
Return newImage
Finally
go.Dispose()
gn.Dispose()
Drawing = False
End Try
End Function
<Description("Clear the rubber box"), _
EditorBrowsable(EditorBrowsableState.Always)> _
Public Sub Clear()
If Drawn Or Drawing Then Remove()
Drawing = False
End Sub
End ClassDa fehlt dann nur noch:
Imports System.Windows.Forms
Public Class RubberBoxEventArgs
Inherits System.EventArgs
Public source As control
Public x As Integer
Public y As Integer
Public width As Integer
Public height As Integer
Public Sub New(ByVal src As Control, ByVal px As Integer, ByVal py As _
Integer, ByVal pwidth As Integer, ByVal pheight As Integer)
source = src
x = px
y = py
width = pwidth
height = pheight
End Sub
End Class |