| 

.NET C# Java Javascript Exception

2
Ich möchte einen Grafikbildauschnitt aus einem Bildfeld oder vom aktuellen Bildschirm (also Inhalte außerhalb des eigenen Programms) über eine Gummibandauswahl selektieren. Der Rahmen dafür soll dynamisch bei gedrückter linker Maustaste aufziehbar sein (rechteckig). Wird die Maustaste gelöst, soll der Rahmen weiterhin selektierbar, verschiebbar und wieder in der Größe änderbar sein. Sobald eine rahmenspezifische Funktion abgerufen wurde, soll der Rahmen wieder verschwinden. Wie lässt sich das in einer Windows-Forms- und einer WPF-Anwendung realisieren?
News:
28.05.2011
1 Antwort
2
Hallo,
ich habe so etwas auch schon mal versucht. Der Code ist noch ein wenig buggy (das Zeichnen der Handles ist noch nicht ganz korrekt), funktioniert aber.
Hier ein Bild:


Zunächst eine Klasse GraphicObjects, in der die Linien und Rechecke gespeichert werden und die das Zeichnen dieser Objekte übernimmt.
Imports System.Drawing.Drawing2D

''' <summary>
''' Klasse für Crop-Objekte
''' </summary>
<System.Runtime.InteropServices.ComVisible(False)> _
Friend Class GraphicObjects

''' <summary>
''' Handle positions
''' </summary>
Friend Enum HandlePositionEnum
UpperLeft
UpperRight
LowerRight
LowerLeft
End Enum


''' <summary>
''' Interface mit gemeinsamen Eigenschaften für alle Objekt-Typen
''' </summary>
Friend Interface GODrawing
Property P1() As Point
Property P2() As Point
Property Selected() As Boolean
ReadOnly Property DrawingPen() As Pen
ReadOnly Property DrawingCursor() As Cursor
Property CropColor() As Color
Sub Draw(ByVal g As Graphics)
Sub Offset(ByVal dx As Integer, ByVal dy As Integer)
End Interface


''' <summary>
''' Klasse für Rechtecke
''' </summary>
<System.Runtime.InteropServices.ComVisible(False)> _
Friend Class CropRectangle
Implements GODrawing

Private mCropColor As Color = Color.Black
Private mPenNormal As New Pen(mCropColor, 1)
Private mPenSelected As New Pen(Color.Red, 3)
Private mDrawingPen As Pen
Private mStartPoint As Point
Private mCornerPoint As Point
Private mSelected As Boolean = False
Private mCursor As Cursor = Cursors.Cross
Private mHandlePosition As String
Private mOutgoingLine As CropLine
Private mIncomingLine As CropLine


Public ReadOnly Property DrawingPen() As Pen Implements GODrawing.DrawingPen
Get
Return mDrawingPen
End Get
End Property

Public Property P1() As Point Implements GODrawing.P1
Get
Return mStartPoint
End Get
Set(ByVal value As Point)
mStartPoint = value
End Set
End Property

Public Property P2() As Point Implements GODrawing.P2
Get
Return mCornerPoint
End Get
Set(ByVal value As Point)
mCornerPoint = value
End Set
End Property

Public Property Selected() As Boolean Implements GODrawing.Selected
Get
Return mSelected
End Get
Set(ByVal value As Boolean)
mSelected = value
If value = True Then
mDrawingPen = mPenSelected
Else
mDrawingPen = mPenNormal
End If
End Set
End Property

Public Property CropColor() As System.Drawing.Color Implements GODrawing.CropColor
Get
Return mCropColor
End Get
Set(ByVal value As System.Drawing.Color)
mCropColor = value
mPenNormal = New Pen(mCropColor, 1)
End Set
End Property

Public ReadOnly Property DrawingCursor() As Cursor Implements GODrawing.DrawingCursor
Get
Return mCursor
End Get
End Property

Public ReadOnly Property HandlePosition() As HandlePositionEnum
Get
Return mHandlePosition
End Get
End Property

Public ReadOnly Property IncomingLine() As CropLine
Get
Return mIncomingLine
End Get
End Property

Public ReadOnly Property OutgoingLine() As CropLine
Get
Return mOutgoingLine
End Get
End Property


''' <summary>
''' Initializes a new instance of the <see cref="CropRectangle" /> class.
''' </summary>
''' <param name="Startpoint">The startpoint.</param>
''' <param name="Cornerpoint">The cornerpoint.</param>
''' <param name="ModifyCursor">The modify cursor.</param>
''' <param name="HandlePos">The handle pos.</param>
''' <param name="OutgoingLine">The outgoing line.</param>
''' <param name="IncomingLine">The incoming line.</param>
Public Sub New(ByVal Startpoint As Point, ByVal Cornerpoint As Point, ByVal ModifyCursor As Cursor, _
ByVal HandlePos As HandlePositionEnum, _
ByVal OutgoingLine As CropLine, ByVal IncomingLine As CropLine, ByVal CropCol As Color)
mDrawingPen = mPenNormal
mStartPoint = Startpoint
mCornerPoint = Cornerpoint
mCursor = ModifyCursor
mHandlePosition = HandlePos
mOutgoingLine = OutgoingLine
mIncomingLine = IncomingLine
mCropColor = CropCol
End Sub

Public Sub Draw(ByVal g As Graphics) Implements GODrawing.Draw
g.FillRectangle(mDrawingPen.Brush, _
New Rectangle(mStartPoint.X, mStartPoint.Y, Math.Abs(mCornerPoint.X - mStartPoint.X), Math.Abs(mCornerPoint.Y - mStartPoint.Y)))
End Sub

Public Sub Offset(ByVal dx As Integer, ByVal dy As Integer) Implements GODrawing.Offset
mStartPoint.Offset(dx, dy)
If mStartPoint.X < -8 Then mStartPoint.X = -8
If mStartPoint.Y < -8 Then mStartPoint.Y = -8
mCornerPoint.Offset(dx, dy)
If mCornerPoint.X < 2 Then mCornerPoint.X = 2
If mCornerPoint.Y < 2 Then mCornerPoint.Y = 2
If mStartPoint.X >= GraphicObjects.CanvasSize.Width + 2 Then mStartPoint.X = GraphicObjects.CanvasSize.Width + 2
If mStartPoint.Y >= GraphicObjects.CanvasSize.Height + 2 Then mStartPoint.Y = GraphicObjects.CanvasSize.Height + 2
If mCornerPoint.X >= GraphicObjects.CanvasSize.Width - 8 Then mCornerPoint.X = GraphicObjects.CanvasSize.Width - 8
If mCornerPoint.Y >= GraphicObjects.CanvasSize.Height - 8 Then mCornerPoint.Y = GraphicObjects.CanvasSize.Height - 8
End Sub
End Class


''' <summary>
''' Klasse für Linien
''' </summary>
<System.Runtime.InteropServices.ComVisible(False)> _
Friend Class CropLine
Implements GODrawing

Private mStartPoint As Point
Private mEndPoint As Point
Private mCropColor As Color = Color.Black
Private mPenNormal As New Pen(New HatchBrush(HatchStyle.DarkDownwardDiagonal, Color.LightGray, mCropColor), 1)
Private mPenSelected As New Pen(Color.Red, 3)
Private mDrawingPen As Pen
Private mSelected As Boolean = False
Private mCursor As Cursor = Cursors.Cross



Public ReadOnly Property DrawingPen() As System.Drawing.Pen Implements GODrawing.DrawingPen
Get
Return mDrawingPen
End Get
End Property

Public Property P1() As System.Drawing.Point Implements GODrawing.P1
Get
Return mStartPoint
End Get
Set(ByVal value As System.Drawing.Point)
mStartPoint = value
End Set
End Property

Public Property P2() As System.Drawing.Point Implements GODrawing.P2
Get
Return mEndPoint
End Get
Set(ByVal value As System.Drawing.Point)
mEndPoint = value
End Set
End Property

Public Property Selected() As Boolean Implements GODrawing.Selected
Get
Return mSelected
End Get
Set(ByVal value As Boolean)
mSelected = value
If value = True Then
mDrawingPen = mPenSelected
Else
mDrawingPen = mPenNormal
End If
End Set
End Property

Public Property CropColor() As System.Drawing.Color Implements GODrawing.CropColor
Get
Return mCropColor
End Get
Set(ByVal value As System.Drawing.Color)
mCropColor = value
mPenNormal = New Pen(New HatchBrush(HatchStyle.DarkDownwardDiagonal, Color.LightGray, mCropColor), 1)
End Set
End Property

Public ReadOnly Property DrawingCursor() As Cursor Implements GODrawing.DrawingCursor
Get
Return mCursor
End Get
End Property


''' <summary>
''' Initializes a new instance of the <see cref="CropLine" /> class.
''' </summary>
''' <param name="np1">The NP1.</param>
''' <param name="np2">The NP2.</param>
''' <param name="StartHandlePos">The start handle pos.</param>
''' <param name="EndHandlePos">The end handle pos.</param>
Public Sub New(ByVal np1 As Point, ByVal np2 As Point, _
ByVal StartHandlePos As HandlePositionEnum, ByVal EndHandlePos As HandlePositionEnum, ByVal CropCol As Color)
mDrawingPen = mPenNormal
mStartPoint = np1
mEndPoint = np2
mCropColor = CropCol
End Sub

Public Sub Draw(ByVal g As Graphics) Implements GODrawing.Draw
g.DrawLine(mDrawingPen, mStartPoint, mEndPoint)
End Sub

Public Sub Offset(ByVal dx As Integer, ByVal dy As Integer) Implements GODrawing.Offset
mStartPoint.Offset(dx, dy)
If mStartPoint.X < 0 Then mStartPoint.X = 0
If mStartPoint.Y < 0 Then mStartPoint.Y = 0
mEndPoint.Offset(dx, dy)
If mEndPoint.X < 0 Then mEndPoint.X = 0
If mEndPoint.Y < 0 Then mEndPoint.Y = 0
If mStartPoint.X >= GraphicObjects.CanvasSize.Width Then mStartPoint.X = GraphicObjects.CanvasSize.Width
If mStartPoint.Y >= GraphicObjects.CanvasSize.Height Then mStartPoint.Y = GraphicObjects.CanvasSize.Height
If mEndPoint.X >= GraphicObjects.CanvasSize.Width Then mEndPoint.X = GraphicObjects.CanvasSize.Width
If mEndPoint.Y >= GraphicObjects.CanvasSize.Height Then mEndPoint.Y = GraphicObjects.CanvasSize.Height
End Sub

Public Sub ScaleStart(ByVal dx As Integer, ByVal dy As Integer)
mStartPoint.Offset(dx, dy)
If mStartPoint.X < 0 Then mStartPoint.X = 0
If mStartPoint.Y < 0 Then mStartPoint.Y = 0
If mStartPoint.X >= GraphicObjects.CanvasSize.Width Then mStartPoint.X = GraphicObjects.CanvasSize.Width
If mStartPoint.Y >= GraphicObjects.CanvasSize.Height Then mStartPoint.Y = GraphicObjects.CanvasSize.Height
End Sub

Public Sub ScaleEnd(ByVal dx As Integer, ByVal dy As Integer)
mEndPoint.Offset(dx, dy)
If mEndPoint.X < 0 Then mEndPoint.X = 0
If mEndPoint.Y < 0 Then mEndPoint.Y = 0
If mEndPoint.X >= GraphicObjects.CanvasSize.Width Then mEndPoint.X = GraphicObjects.CanvasSize.Width
If mEndPoint.Y >= GraphicObjects.CanvasSize.Height Then mEndPoint.Y = GraphicObjects.CanvasSize.Height
End Sub
End Class



''' <summary>
''' Liste der Crop-Objekte
''' </summary>
Friend Shared CropObjects As List(Of GODrawing)

''' <summary>
''' Pointer to crop rectangles
''' </summary>
Friend Shared CropRectUpperLeft As CropRectangle
Friend Shared CropRectUpperRight As CropRectangle
Friend Shared CropRectLowerLeft As CropRectangle
Friend Shared CropRectLowerRight As CropRectangle

''' <summary>
''' Size of the drawing canvas
''' </summary>
Friend Shared CanvasSize As New Size


''' <summary>
''' Initializes a new instance of the <see cref="GraphicObjects" /> class.
''' </summary>
Friend Sub New()
CropObjects = New List(Of GODrawing)
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
CropObjects.Clear()
CropObjects = Nothing
End Sub


''' <summary>
''' Selects the lines.
''' </summary>
Shared Sub SelectLines()
For Each go As GODrawing In CropObjects
If TypeOf go Is CropLine Then go.Selected = True
Next
End Sub

''' <summary>
''' Deselects all.
''' </summary>
Shared Sub DeselectAll()
For Each go As GODrawing In CropObjects
go.Selected = False
Next
End Sub

''' <summary>
''' Moves CropObjects.
''' </summary>
''' <param name="dx">The dx.</param>
''' <param name="dy">The dy.</param>
Shared Sub Offset(ByVal dx As Integer, ByVal dy As Integer)
For Each go As GODrawing In CropObjects
go.Offset(-dx, -dy)
Next
End Sub

''' <summary>
''' Scales CropObjects.
''' </summary>
''' <param name="dx">The dx.</param>
''' <param name="dy">The dy.</param>
Shared Sub Scale(ByVal dx As Integer, ByVal dy As Integer)
' Schleife über alle
For Each go As GODrawing In CropObjects
' je nach Typ
If TypeOf go Is CropRectangle Then
Dim cr As CropRectangle = go
' selektiertes (Rechteck) ...
If cr.Selected Then
' ... verschieben
cr.Offset(-dx, -dy)

' angrenzende Objekte verschieben
Select Case cr.HandlePosition
Case HandlePositionEnum.UpperLeft
GraphicObjects.CropRectUpperRight.Offset(0, -dy)
GraphicObjects.CropRectLowerLeft.Offset(-dx, 0)
cr.OutgoingLine.ScaleStart(-dx, -dy)
cr.OutgoingLine.ScaleEnd(0, -dy)
cr.IncomingLine.ScaleStart(-dx, 0)
cr.IncomingLine.ScaleEnd(-dx, -dy)
GraphicObjects.CropRectUpperRight.OutgoingLine.ScaleStart(0, -dy)
GraphicObjects.CropRectLowerLeft.IncomingLine.ScaleEnd(-dx, 0)
Case HandlePositionEnum.UpperRight
GraphicObjects.CropRectUpperLeft.Offset(0, -dy)
GraphicObjects.CropRectLowerRight.Offset(-dx, 0)
cr.OutgoingLine.ScaleStart(-dx, -dy)
cr.OutgoingLine.ScaleEnd(-dx, 0)
cr.IncomingLine.ScaleStart(0, -dy)
cr.IncomingLine.ScaleEnd(-dx, -dy)
GraphicObjects.CropRectUpperLeft.IncomingLine.ScaleEnd(0, -dy)
GraphicObjects.CropRectLowerRight.OutgoingLine.ScaleStart(-dx, 0)
Case HandlePositionEnum.LowerRight
GraphicObjects.CropRectUpperRight.Offset(-dx, 0)
GraphicObjects.CropRectLowerLeft.Offset(0, -dy)
cr.OutgoingLine.ScaleStart(-dx, -dy)
cr.OutgoingLine.ScaleEnd(0, -dy)
cr.IncomingLine.ScaleStart(-dx, 0)
cr.IncomingLine.ScaleEnd(-dx, -dy)
GraphicObjects.CropRectUpperRight.IncomingLine.ScaleEnd(-dx, 0)
GraphicObjects.CropRectLowerLeft.OutgoingLine.ScaleStart(0, -dy)
Case HandlePositionEnum.LowerLeft
GraphicObjects.CropRectUpperLeft.Offset(-dx, 0)
GraphicObjects.CropRectLowerRight.Offset(0, -dy)
cr.OutgoingLine.ScaleStart(-dx, -dy)
cr.OutgoingLine.ScaleEnd(-dx, 0)
cr.IncomingLine.ScaleStart(0, -dy)
cr.IncomingLine.ScaleEnd(-dx, -dy)
GraphicObjects.CropRectUpperLeft.OutgoingLine.ScaleStart(-dx, 0)
GraphicObjects.CropRectLowerRight.IncomingLine.ScaleEnd(0, -dy)
End Select

' fertig
Exit For
End If
End If
Next
End Sub
End Class


Dann gibt es natürlich ein Formular mit einer PictureBox. Zunächst ein paar globale Variablen:
#Region "private members"
Private mGraphicObjects As New GraphicObjects
Private mCropMouseButtonDown As Boolean = False
Private mCropStartPos As Point
Private mCropEndPos As Point
Private mCropMoving As Boolean = False
Private mCropScaling As Boolean = False
Private mNeedToSave As Boolean = False
#End Region


Und die Maus-Events:
''' <summary>
''' Handles the MouseDown event of the pBox control.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs" /> instance containing the event data.</param>
Private Sub pBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pBox.MouseDown
' nur bei linker Maustaste
If e.Button = Windows.Forms.MouseButtons.Left Then
' Start des Verschiebens/Modifizierens
mCropMouseButtonDown = True
mCropStartPos = e.Location
End If
End Sub

''' <summary>
''' Handles the MouseUp event of the pBox control.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs" /> instance containing the event data.</param>
Private Sub pBox_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pBox.MouseUp
' nur bei linker Maustaste
If e.Button = Windows.Forms.MouseButtons.Left Then
' Crop platzieren oder schon vorhanden?
If GraphicObjects.CropObjects.Count = 0 Then
' neu platzieren
pm_createCrop(mCropStartPos, mCropEndPos)
Else
' Ende des Verschiebens/Modifizierens
mCropMoving = False
mCropScaling = False
End If

' Aktion beenden
mCropMouseButtonDown = False
End If
End Sub

''' <summary>
''' Handles the MouseMove event of the pBox control.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs" /> instance containing the event data.</param>
Private Sub pBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pBox.MouseMove
Dim gp As Drawing2D.GraphicsPath


' Mauskoordinaten anzeigen
tsslMousePos.Text = String.Format(" {0:0} x {1:0}", e.Location.X * pBox.xScaleFactor, e.Location.Y * pBox.xScaleFactor)

' weiter nur, falls zuschneiden aktiv
If Not kcbAutoCrop.Checked Then Exit Sub

' Crop platzieren oder schon vorhanden?
If GraphicObjects.CropObjects.Count = 0 Then
If mCropMouseButtonDown Then
' Position für neuen Crop merken
mCropEndPos = e.Location

' Mauskoordinaten eventuell korrigieren
pm_correctCrop(mCropEndPos)
End If
Else
' Welcher Modus?
If mCropMoving Then
' -- Verschieben
GraphicObjects.Offset(mCropStartPos.X - e.X, mCropStartPos.Y - e.Y)
mCropStartPos = e.Location
ElseIf mCropScaling Then
' -- Skalieren
GraphicObjects.Scale(mCropStartPos.X - e.X, mCropStartPos.Y - e.Y)
mCropStartPos = e.Location
Else
' -- HitTest
' - auf Linien zum Verschieben
gp = New Drawing2D.GraphicsPath
For Each go As GraphicObjects.GODrawing In GraphicObjects.CropObjects
If TypeOf go Is GraphicObjects.CropLine Then gp.AddLine(go.P1, go.P2)
Next
If gp.IsOutlineVisible(e.Location, New Pen(Color.Black, 5)) Then
' Darstellung
GraphicObjects.SelectLines()
sender.Cursor = Cursors.SizeAll

' Verschieben-Modus anschalten
If mCropMouseButtonDown Then
mCropMoving = True
Else
mCropMoving = False
End If
Else
GraphicObjects.DeselectAll()
sender.Cursor = Cursors.Cross
End If


' - HitTest auf Rechtecke zum Modifizieren
gp = New Drawing2D.GraphicsPath
For Each go As GraphicObjects.GODrawing In GraphicObjects.CropObjects
If TypeOf go Is GraphicObjects.CropRectangle Then
gp.AddRectangle(New Rectangle(go.P1.X, go.P1.Y, Math.Abs(go.P2.X - go.P1.X), Math.Abs(go.P2.Y - go.P1.Y)))
gp.AddRectangle(New Rectangle(go.P1.X + 2, go.P1.Y + 2, Math.Abs(go.P2.X - go.P1.X) - 4, Math.Abs(go.P2.Y - go.P1.Y) - 4))
End If
If gp.IsOutlineVisible(e.Location, New Pen(Color.Black, 5)) Then
' Darstellung
go.Selected = True
sender.Cursor = go.DrawingCursor

' Skalieren-Modus anschalten
If mCropMouseButtonDown Then
mCropScaling = True
Else
mCropScaling = False
End If
Exit For
End If
Next
End If
End If

' neuzeichnen
pBox.Refresh()
End Sub


Und noch drei Funktionen:
''' <summary>
''' Creates the default crop.
''' </summary>
''' <param name="StartPoint">The start point.</param>
''' <param name="EndPoint">The end point.</param>
Private Sub pm_createCrop(ByVal StartPoint As Point, ByVal EndPoint As Point)
' Liste der Objekte leeren
GraphicObjects.CropObjects.Clear()

' Rectangle verwenden
Dim rect As Rectangle = pm_createCropRectangle(StartPoint, EndPoint)
Dim Corner As New Point(rect.Location.X + rect.Width, rect.Location.Y + rect.Height)

' vier Linien
Dim cl1 As New GraphicObjects.CropLine(rect.Location, New Point(Corner.X, rect.Location.Y), GraphicObjects.HandlePositionEnum.UpperLeft, GraphicObjects.HandlePositionEnum.UpperRight, kcbCropColor.SelectedColor)
GraphicObjects.CropObjects.Add(cl1)
Dim cl2 As New GraphicObjects.CropLine(New Point(Corner.X, rect.Location.Y), Corner, GraphicObjects.HandlePositionEnum.UpperRight, GraphicObjects.HandlePositionEnum.LowerRight, kcbCropColor.SelectedColor)
GraphicObjects.CropObjects.Add(cl2)
Dim cl3 As New GraphicObjects.CropLine(Corner, New Point(rect.Location.X, Corner.Y), GraphicObjects.HandlePositionEnum.LowerRight, GraphicObjects.HandlePositionEnum.LowerLeft, kcbCropColor.SelectedColor)
GraphicObjects.CropObjects.Add(cl3)
Dim cl4 As New GraphicObjects.CropLine(New Point(rect.Location.X, Corner.Y), rect.Location, GraphicObjects.HandlePositionEnum.LowerLeft, GraphicObjects.HandlePositionEnum.UpperLeft, kcbCropColor.SelectedColor)
GraphicObjects.CropObjects.Add(cl4)

' vier Handles
Dim cr As GraphicObjects.CropRectangle
cr = New GraphicObjects.CropRectangle(New Point(rect.Location.X - 8, rect.Location.Y - 8), New Point(rect.Location.X + 2, rect.Location.Y + 2), Cursors.SizeNWSE, GraphicObjects.HandlePositionEnum.UpperLeft, cl1, cl4, kcbCropColor.SelectedColor)
GraphicObjects.CropObjects.Add(cr)
GraphicObjects.CropRectUpperLeft = cr

cr = New GraphicObjects.CropRectangle(New Point(Corner.X - 2, rect.Location.Y - 8), New Point(Corner.X + 8, rect.Location.Y + 2), Cursors.SizeNESW, GraphicObjects.HandlePositionEnum.UpperRight, cl2, cl1, kcbCropColor.SelectedColor)
GraphicObjects.CropObjects.Add(cr)
GraphicObjects.CropRectUpperRight = cr

cr = New GraphicObjects.CropRectangle(New Point(Corner.X - 2, Corner.Y - 2), New Point(Corner.X + 8, Corner.Y + 8), Cursors.SizeNWSE, GraphicObjects.HandlePositionEnum.LowerRight, cl3, cl2, kcbCropColor.SelectedColor)
GraphicObjects.CropObjects.Add(cr)
GraphicObjects.CropRectLowerRight = cr

cr = New GraphicObjects.CropRectangle(New Point(rect.Location.X - 8, Corner.Y - 2), New Point(rect.Location.X + 2, Corner.Y + 8), Cursors.SizeNESW, GraphicObjects.HandlePositionEnum.LowerLeft, cl4, cl3, kcbCropColor.SelectedColor)
GraphicObjects.CropObjects.Add(cr)
GraphicObjects.CropRectLowerLeft = cr
End Sub

''' <summary>
''' Corrects the crop point.
''' </summary>
''' <param name="pt">The pt.</param>
Private Sub pm_correctCrop(ByRef pt As Point)
If pt.X < 0 Then pt.X = 0
If pt.Y < 0 Then pt.Y = 0
If pt.X >= pBox.Image.Width Then pt.X = pBox.Image.Width
If pt.Y >= pBox.Image.Height Then pt.Y = pBox.Image.Height
End Sub

''' <summary>
''' Creates the crop rectangle.
''' </summary>
''' <param name="P1">The p1.</param>
''' <param name="p2">The p2.</param>
''' <returns></returns>
Private Function pm_createCropRectangle(ByVal P1 As Point, ByVal p2 As Point) As Rectangle
Dim rect As New Rectangle


' welcher Quadrant?
If p2.X >= P1.X And p2.Y >= P1.Y Then
' rechts unten
rect.Location = P1
rect.Width = p2.X - P1.X
rect.Height = p2.Y - P1.Y
ElseIf p2.X < P1.X And p2.Y < P1.Y Then
' links unten
rect.Location = p2
rect.Width = P1.X - p2.X
rect.Height = P1.Y - p2.Y
ElseIf p2.X < P1.X And p2.Y >= P1.Y Then
' links oben
rect.X = p2.X
rect.Y = P1.Y
rect.Width = P1.X - p2.X
rect.Height = p2.Y - P1.Y
Else
' rechts oben
rect.X = P1.X
rect.Y = p2.Y
rect.Width = p2.X - P1.X
rect.Height = P1.Y - p2.Y
End If

' erzeugtes Rectangle zurückgegeben
Return rect
End Function


Der Paint-Event der PicBox:
''' <summary>
''' Handles the Paint event of the pBox control.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="System.Windows.Forms.PaintEventArgs" /> instance containing the event data.</param>
Private Sub pBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pBox.Paint
' fertiges Crop zeichnen?
If GraphicObjects.CropObjects.Count > 0 Then
For Each cl As GraphicObjects.GODrawing In GraphicObjects.CropObjects
cl.Draw(e.Graphics)
Next
Exit Sub
End If

' Crop wird gerade erstellt
If mCropMouseButtonDown Then
e.Graphics.DrawRectangle(New Pen(New Drawing2D.HatchBrush(Drawing2D.HatchStyle.DarkDownwardDiagonal, Color.LightGray), 1), _
pm_createCropRectangle(mCropStartPos, mCropEndPos))
End If
End Sub


Ich hoffe, ich habe nichts vergessen, sonst einfach melden.
Liebe Grüße, Tom
10.06.2011
Metal-Frog 946 1 7

Stelle deine .net-Frage jetzt!
TOP TECHNOLOGIES CONSULTING GmbH