Reputation: 777
I want to place my Crop Control on center of image of picture box. I have tried following code
Dim oCropControl As new CropControl
Dim oControlLocation As Point
oControlLocation = New Point(peImageViewer.Width / 2, peImageViewer.Height / 2)
oCropControl.Location = New Point(oControlLocation.X, oControlLocation.Y)
But this is not working well.. :( Crop Control shown in bottom.
Thanks in advance!!
Upvotes: 1
Views: 1685
Reputation: 9981
Assuming they are both parented to the same control you could do it like this:
Dim rect1 As Rectangle = Me.myPictureBox.Bounds
Dim rect2 As Rectangle = Me.myCropControl.Bounds
rect2.X = CInt(rect1.X + ((rect1.Width / 2) - (rect2.Width / 2)))
rect2.Y = CInt(rect1.Y + ((rect1.Height / 2) - (rect2.Height / 2)))
Me.myCropControl.Bounds = rect2
Me.myCropControl.BringToFront()
Example
Public Class Form1
Public Sub New()
Me.InitializeComponent()
Me.Size = New Size(400, 400)
Me.StartPosition = FormStartPosition.CenterScreen
Me.myButton = New Button() With {.Location = New Point(3, 3), .Text = "ALIGN!"}
Me.myCropControl = New Label() With {.Bounds = New Rectangle(245, 263, 60, 60), .BackColor = Color.Blue, .ForeColor = Color.White, .Text = "CROP", .TextAlign = ContentAlignment.MiddleCenter}
Me.myPictureBox = New PictureBox() With {.Bounds = New Rectangle(23, 56, 246, 143), .BackColor = Color.Red}
Me.Controls.AddRange({Me.myButton, Me.myCropControl, Me.myPictureBox})
End Sub
Private Sub Align(sender As Object, e As EventArgs) Handles myButton.Click
Dim rect1 As Rectangle = Me.myPictureBox.Bounds
Dim rect2 As Rectangle = Me.myCropControl.Bounds
rect2.X = CInt(rect1.X + ((rect1.Width / 2) - (rect2.Width / 2)))
rect2.Y = CInt(rect1.Y + ((rect1.Height / 2) - (rect2.Height / 2)))
Me.myCropControl.Bounds = rect2
Me.myCropControl.BringToFront()
End Sub
Private WithEvents myButton As Button
Private myCropControl As Label
Private myPictureBox As PictureBox
End Class
Upvotes: 2