Выделение участка изображения в PictureBox - Visual Basic .NET
Формулировка задачи:
Привет всем!
Хотел бы реализовать функцию, которая бы позволяла на изображении в PictureBox менять размер и положение участка выделения (прямоугольника) с помощью мыши, то есть, например, как в Paint или любом другом графическом редакторе. На изображении стоит прямоугольник и, тягая за его края, можно было бы менять размер этого прямоугольника. Рисование самого прямоугольника делаю так:
где
Листинг программы
- Dim g As Graphics
- Dim bmp As New Bitmap(PictureBox1.Image)
- g = Graphics.FromImage(bmp)
- Dim pen As New Pen(Color.Red, 3)
- g.DrawRectangle(pen, rect)
rect
- это тип Rectangle, определяющий его границы. Как можно мышь к прямоугольнику привязать, чтобы менять его размеры, подскажите, пожалуйста? Спасибо!Решение задачи: «Выделение участка изображения в PictureBox»
textual
Листинг программы
- Public Class RubberBand
- ' ----- The three types of rubber bands.
- Public Enum RubberBandStyle
- DashedLine
- ThickLine
- SolidBox
- SolidBoxWithDashedLine
- End Enum
- ' ----- The current drawing state.
- Public Enum RubberBandState
- Inactive
- FirstTime
- Active
- End Enum
- ' ----- Class-level variables.
- Private BasePoint As Point
- Private ExtentPoint As Point
- Private CurrentState As RubberBandState
- Private BaseControl As Control
- Public Style As RubberBandStyle
- Public BackColor As Color
- Public Sub New(ByVal useControl As Control, _
- Optional ByVal useStyle As RubberBandStyle = _
- RubberBandStyle.DashedLine)
- ' ----- Constructor with one or two parameters.
- BaseControl = useControl
- Style = useStyle
- BackColor = Color.Black
- End Sub
- Public Sub New(ByVal useControl As Control, ByVal useStyle As RubberBandStyle, ByVal useColor As Color)
- ' ----- Constructor with three parameters.
- BaseControl = useControl
- Style = useStyle
- BackColor = useColor
- End Sub
- Public ReadOnly Property Rectangle() As Rectangle
- Get
- ' ----- Return the bounds of the rubber-band area.
- Dim result As Rectangle
- ' ----- Ensure the coordinates go left to
- ' right, top to bottom.
- result.X = IIf(BasePoint.X < ExtentPoint.X, _
- BasePoint.X, ExtentPoint.X)
- result.Y = IIf(BasePoint.Y < ExtentPoint.Y, _
- BasePoint.Y, ExtentPoint.Y)
- result.Width = Math.Abs(ExtentPoint.X - BasePoint.X)
- result.Height = Math.Abs(ExtentPoint.Y - BasePoint.Y)
- Return result
- End Get
- End Property
- Public Sub Start(ByVal x As Integer, ByVal y As Integer)
- ' ----- Start drawing the rubber band. The user must
- ' call Stretch() to actually draw the first
- ' band image.
- BasePoint.X = x
- BasePoint.Y = y
- ExtentPoint.X = x
- ExtentPoint.Y = y
- Normalize(BasePoint)
- CurrentState = RubberBandState.FirstTime
- End Sub
- Public Sub Stretch(ByVal x As Integer, ByVal y As Integer)
- ' ----- Change the size of the rubber band.
- Dim newPoint As Point
- ' ----- Prepare the new stretch point.
- newPoint.X = x
- newPoint.Y = y
- Normalize(newPoint)
- Select Case CurrentState
- Case RubberBandState.Inactive
- ' ----- Rubber band not in use.
- Return
- Case RubberBandState.FirstTime
- ' ----- Draw the initial rubber band.
- ExtentPoint = newPoint
- DrawTheRectangle()
- CurrentState = RubberBandState.Active
- Case RubberBandState.Active
- ' ----- Undraw the previous band, then
- ' draw the new one.
- DrawTheRectangle()
- ExtentPoint = newPoint
- DrawTheRectangle()
- End Select
- End Sub
- Public Sub Finish()
- ' ----- Stop drawing the rubber band.
- DrawTheRectangle()
- CurrentState = 0
- End Sub
- Private Sub Normalize(ByRef whichPoint As Point)
- ' ----- Don't let the rubber band go outside the view.
- If (whichPoint.X < 0) Then whichPoint.X = 0
- If (whichPoint.X >= BaseControl.ClientSize.Width) Then whichPoint.X = BaseControl.ClientSize.Width - 1
- If (whichPoint.Y < 0) Then whichPoint.Y = 0
- If (whichPoint.Y >= BaseControl.ClientSize.Height) Then whichPoint.Y = BaseControl.ClientSize.Height - 1
- End Sub
- Private Sub DrawTheRectangle()
- ' ----- Draw the rectangle on the control or
- ' form surface.
- Dim drawArea As Rectangle
- Dim screenStart, screenEnd As Point
- ' ----- Get the square that is the rubber-band area.
- screenStart = BaseControl.PointToScreen(BasePoint)
- screenEnd = BaseControl.PointToScreen(ExtentPoint)
- drawArea.X = screenStart.X
- drawArea.Y = screenStart.Y
- drawArea.Width = (screenEnd.X - screenStart.X)
- drawArea.Height = (screenEnd.Y - screenStart.Y)
- ' ----- Draw using the user-selected style.
- Select Case Style
- Case RubberBandStyle.DashedLine
- ControlPaint.DrawReversibleFrame( _
- drawArea, BackColor, FrameStyle.Dashed)
- Case RubberBandStyle.ThickLine
- ControlPaint.DrawReversibleFrame( _
- drawArea, BackColor, FrameStyle.Thick)
- Case RubberBandStyle.SolidBox
- ControlPaint.FillReversibleRectangle( _
- drawArea, BackColor)
- Case RubberBandStyle.SolidBoxWithDashedLine
- ControlPaint.FillReversibleRectangle( _
- drawArea, BackColor)
- ControlPaint.DrawReversibleFrame( _
- drawArea, Color.Black, FrameStyle.Dashed)
- End Select
- End Sub
- End Class
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д