HOW TO ADJUST THE SIZE OF DRAWN RECTANGLE AT RUNTIME

Madhu Mitha

Total Post:31

Points:217
Posted by  Madhu Mitha
VB.Net 
 961  View(s)
Ratings:
Rate this:
hi,,


   it is possible to adjust the size of a rectangle which is drawn in a picturebox with drawrectangle function.. During run time i want to adjust my rectangle width using numericupdown control.. 


thank u
  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: how to adjust the size of drawn rectangle at runtime

    I have some code that might help you:


    ' Rectangle to draw

    Private Rct As New Rectangle(0, 0, 0, 0)

    Private offsetX As Integer = 0

    Private offsetY As Integer = 0

     

    Sub Main() Handles MyBase.Load

     

        ' Some image to use

        MiniPictureBox.Image = My.Resources.P6130003

        MainPictureBox.Image = My.Resources.P6130003

     

    End Sub

     

    Private Sub MiniPictureBox_MouseDown(sender As Object, e As MouseEventArgs) Handles MiniPictureBox.MouseDown

     

        If e.Button = Windows.Forms.MouseButtons.Left Then

     

            If Not Rct.Contains(e.Location) Then

                ' New rectangle

                Rct.Location = New Point(e.X, e.Y)

            Else

                ' Moving a rectangle

                offsetX = Rct.X - e.X

                offsetY = Rct.Y - e.Y

            End If

     

        ElseIf e.Button = Windows.Forms.MouseButtons.Right Then

     

            ' Clears the screen of a rectangle

            Rct = New Rectangle(0, 0, 0, 0)

            MiniPictureBox.Invalidate()

     

        End If

     

    End Sub

     

    Private Sub MiniPictureBox_MouseMove(sender As Object, e As MouseEventArgs) Handles MiniPictureBox.MouseMove

     

        ' Event handler to update the picture of the rectangle

        If e.Button = Windows.Forms.MouseButtons.Left Then

     

            If Rct.Contains(e.Location) Then

                ' Move the box

                Rct.X = e.X + offsetX

                Rct.Y = e.Y + offsetY

                MainPictureBox.Invalidate()

            Else

                ' Update the size of the box

                Rct.Width = e.X - Rct.X

                Rct.Height = e.Y - Rct.Y

            End If

     

            MiniPictureBox.Invalidate()

     

        End If

     

     

    End Sub

     

    Private Sub MiniPictureBox_MouseUp(sender As Object, e As MouseEventArgs) Handles MiniPictureBox.MouseUp

     

        ' Event handler to call the paint event for runtime display

        MiniPictureBox.Invalidate()

        MainPictureBox.Invalidate()

     

    End Sub

     

    Private Sub MiniPictureBox_Paint(sender As Object, e As PaintEventArgs) Handles MiniPictureBox.Paint

     

        Dim myPen As Pen = New Pen(Brushes.Red, 2)

        e.Graphics.DrawRectangle(myPen, Rct)

     

    End Sub

     

    Private Sub MainPictureBox_Paint(sender As Object, e As PaintEventArgs) Handles MainPictureBox.Paint

     

        If Rct.Width > 0 Then

            Dim biggerRec As Rectangle = CalculateRectangle(MainPictureBox)

     

            Dim myPen As Pen = New Pen(Brushes.Red, 2)

            e.Graphics.DrawRectangle(myPen, biggerRec)

        End If

     

    End Sub

     

    Private Function CalculateRectangle(currentPicture As PictureBox) As Rectangle

     

        Try

            Dim newWidth As Integer = (Rct.Width / MiniPictureBox.Width) * currentPicture.Image.Width

            Dim newHeight As Integer = (Rct.Height / MiniPictureBox.Height) * currentPicture.Image.Height

            Dim newX As Integer = (Rct.X / MiniPictureBox.Width) * currentPicture.Image.Width

            Dim newY As Integer = (Rct.Y / MiniPictureBox.Height) * currentPicture.Image.Height

            Return New Rectangle(newX, newY, newWidth, newHeight)

        Catch ex As Exception

            MessageBox.Show(ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace)

        End Try

     

    End Function


    This code will allow you to create, move, and clear a rectangle. One point of caution is in the calculations for changing the size of the rectangle, you have to ensure exception handling is properly inserted for any arithmetic exceptions.

  1. Madhu Mitha

    Post:31

    Points:217
    Re: how to adjust the size of drawn rectangle at runtime

    Hai..

    Kamlakar Singh,

                    I tried your code but what i needed is that... When i press my numeric updown control the width of my rectangle should be increased..

    For Example..
    if i created a rectangle with code
    Dim width=50 
    Dim height=25
    Dim rect As New Rectangle(0, 0, width,height)
     g.DrawRectangle(Pens.Black, rect)
    Now, When I press NumericUpDown control the width of my rectangle should be increased as 51,52 53, etc..
    Is thr any possible way to do it..

    Thank U

  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: how to adjust the size of drawn rectangle at runtime

    According to my understanding about your problem scenario, you want to increase height and width according to the numericupdown control on click

    Try this code:

    Private Sub numericUpDown1_ValueChanged(sender As Object, e As EventArgs)

                    Dim aa As Integer = Convert.ToInt32(numericUpDown1.Value.ToString())

                    pictureBox1.Width = aa

                    pictureBox1.Height = aa

    End Sub

     

    I hope it will work for u !! J

  1. Madhu Mitha

    Post:31

    Points:217
    Re: how to adjust the size of drawn rectangle at runtime

    hai,,



           thank u it worked well...

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!