Question

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!!

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top