Solved

Drawing a Bounding Box rectangle in vb.net 2005

Posted on 2009-05-14
4
1,506 Views
Last Modified: 2013-11-27
Hello,

I never thought I'd actually be writing on how to do this, but I am having nothing but trouble getting this to work.

I have a picture on the screen.  I want to give the user the ability to crop the picture, so I have a crop button on the toolbar.  When cropping starts, I want the user to be able to click and drag around the area that they want to keep within the picture.  I would like to draw a rectangle AS they move the mouse.

What I'm running into is I can do the draw rectangle, but as the mouse moves, another rectangle is created and another, etc. so I end up with a series of rectangles rather than one when I'm all done.

In vb6 world, I used a "shape" to accomplish this and just kept moving the bottom right point of the shape.  I just don't know how to do this in vb.net 2005

Thanks so much
Gary H.
0
Comment
Question by:garyhoffmann
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 24389440
Bare bones "rubberbanding" example in VB.Net 2005:
Public Class Form1
 
    Private RC As Rectangle
    Private screenPtA, screenPtB As Point
 
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim pb As PictureBox = CType(sender, PictureBox)
            Cursor.Clip = pb.RectangleToScreen(pb.ClientRectangle)
            screenPtA = Cursor.Position
            screenPtB = screenPtA
            RC = NormalizedRC(screenPtA, screenPtB)
            ControlPaint.DrawReversibleFrame(RC, Color.Black, FrameStyle.Dashed)
        End If
    End Sub
 
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            ControlPaint.DrawReversibleFrame(RC, Color.Black, FrameStyle.Dashed)
            screenPtB = Cursor.Position
            RC = NormalizedRC(screenPtA, screenPtB)
            ControlPaint.DrawReversibleFrame(RC, Color.Black, FrameStyle.Dashed)
        End If
    End Sub
 
    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Cursor.Clip = Nothing
            Dim pb As PictureBox = CType(sender, PictureBox)
            RC = pb.RectangleToClient(RC)
 
            MessageBox.Show(RC.ToString, "Client Crop Rectangle")
        End If
    End Sub
 
    Public Function NormalizedRC(ByVal ptA As Point, ByVal ptB As Point) As Rectangle
        Return New Rectangle(Math.Min(ptA.X, ptB.X), Math.Min(ptA.Y, ptB.Y), Math.Abs(ptA.X - ptB.X), Math.Abs(ptA.Y - ptB.Y))
    End Function
 
End Class

Open in new window

0
 

Author Comment

by:garyhoffmann
ID: 24394485
Idle Mind - this is great.  Thank you.  But... (and there's always a but....)

What if the user realizes they started in the wrong place and wants to start over?  How do I remove the rectangle that's been drawn?

Thank you.
Gary.
0
 

Author Comment

by:garyhoffmann
ID: 24394656
Idle Mind,

Never mind - I found my own answer based on your code.  Thanks again.

Gary
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24396049
To clear the PB just issue a Refresh() and any existing bounding box will be cleared:

    PictureBox1.Refresh()
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question