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..
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
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 MiniPictureBoMouseDown(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 MiniPictureBoMouseMove(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 MiniPictureBoMouseUp(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 MiniPictureBoPaint(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 MainPictureBoPaint(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.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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:
I hope it will work for u !! J
' Rectangle to draw
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.