Schau dir mal folgende Klasse an, damit kannst du das machen.
Public Class RubberBox
Private Visible As Boolean = False
Private Cancel As Boolean = False
Private Position As Point
Private Size As Size
Private OriginalPosition As Point
Private newImage As Bitmap
Private cut As Int16 = 1
Private Sub Draw()
Me.Visible = True
ControlPaint.DrawReversibleFrame(New Rectangle(Position, Size), _
Color.White, FrameStyle.Dashed)
End Sub
Private Sub Remove()
If Visible Then
ControlPaint.DrawReversibleFrame(New Rectangle(Position, Size), _
Color.White, FrameStyle.Dashed)
End If
Visible = False
End Sub
Public Sub MouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
If Not Cancel 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 > Picture.PictureBox.ClientSize.Width Then x = _
Picture.PictureBox.ClientSize.Width
If y < 0 Then y = 0
If y > Picture.PictureBox.ClientSize.Height Then y = _
Picture.PictureBox.ClientSize.Height
'neues Rechteck zeichnen
Size = New Size(x - OriginalPosition.X, y - OriginalPosition.Y)
Draw()
End If
End Sub
Public Sub MouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
Remove()
Cancel = False
OriginalPosition = New Point(e.X, e.Y)
Position = Picture.PictureBox.PointToScreen(OriginalPosition)
Size = New Size(0, 0)
'Startrechteck zeichnen
Draw()
End Sub
Public Sub MouseUp()
If Cancel Then Exit Sub
'Dim x0, y0, x1, y1, tmp As Integer
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
Dim Ratio As Double = Picture.PictureBox.Image.Width / _
Picture.PictureBox.Width
x0 *= Ratio
x1 *= Ratio
y0 *= Ratio
y1 *= Ratio
If x1 - x0 = 0 Or y1 - y0 = 0 Then
Remove()
Else
newImage = New Bitmap(x1 - x0, y1 - y0)
Dim g As Graphics = Graphics.FromImage(newImage)
g.DrawImage(Picture.PictureBox.Image, New Rectangle(0, 0, x1 - _
x0, y1 - y0), New Rectangle(x0, y0, x1 - x0, y1 - y0), _
GraphicsUnit.Pixel)
newImage.SetResolution( _
Picture.PictureBox.Image.HorizontalResolution, _
Picture.PictureBox.Image.VerticalResolution)
g.Dispose()
End If
End Sub
Public Sub CutSelection()
If Not Visible Then Exit Sub
Picture.PictureBox.Image = newImage
Remove()
End Sub
Public Sub Leave()
Remove()
Cancel = True
End Sub
Public Sub KeyDown()
Remove()
Cancel = True
End Sub
End Class Einfach in der Form mit der PictureBox eine Instanze der Klasse initialisieren und danach in den einzelnen Mausaktionen die entsprechenden Prozeduren aufrufen. Hab das deshalb in eine eigene Klasse gesteckt, damit man diese Funktion leichter ein- und ausschalten kann. Die Prizedur CutSelection schneidet den markierten Teil dann aus und zeigt nur noch diesen an.
Du musst sämtliche PictureBox Verweise (Picture.PictureBox) auf die PictureBox umschreiben, die du verwendest.
Bei Fragen einfach melden!
Cu leth |