Solved

Drawing a Bounding Box rectangle in vb.net 2005

Posted on 2009-05-14
4
1,492 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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…

785 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