Find the center of a .NET user control

I would like to display an image in the center of my user control, but I draw a space, actually "finding" the center of the control!

I feel that it should be something very simple, but I just can't get around it.

Ideally, I would like to get the X and Y coordinates that can be added to the Point instance to display at that point.

+3
source share
2 answers

You can just use ctrl.Width / 2, ctrl.Height / 2.
You can add ctrl.Leftand ctrl.Top.

+6
source

If the image should be centered, use this code: (Pseudo, untested)

image.Location = new Point((control.Width / 2) - (image.Width /2),(control.Height / 2) - (image.Height / 2));
+5
source

Source: https://habr.com/ru/post/1785250/


All Articles