Solved

Drawing a Bounding Box rectangle in vb.net 2005

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

16 Experts available now in Live!

Get 1:1 Help Now