Solved

Drawing a Bounding Box rectangle in vb.net 2005

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now