|
-
May 19th, 2026, 06:37 AM
#1
Thread Starter
New Member
Position of a PictureBox
Hello, i want to center a picturebox but i dont understand the maths
For exemple, i set the form size to 400*200 and the picture size to 300*100, so the picture location should be 50*50, but its no centered (it s ok at location 40*30)
What is wrong in my maths ?
Thanks
-
May 19th, 2026, 07:00 AM
#2
Thread Starter
New Member
Re: Position of a PictureBox
-
May 19th, 2026, 10:53 AM
#3
Re: Position of a PictureBox
If you add an image in Quick Reply, it doesn't show up. You'd have to Go Advanced.
However, your math doesn't seem so bad, so you probably need to look at a few other things first. Is the form really the size you are expecting it to be? Probably not. I think that DPI scaling may influence the actual size.
My usual boring signature: Nothing
 
-
May 19th, 2026, 11:53 AM
#4
Re: Position of a PictureBox
This looks correct,
Code:
Public Class Form1
Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles Me.ResizeEnd
CenterPB1()
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
CenterPB1()
End Sub
Private Sub CenterPB1()
PictureBox1.Left = (Me.ClientSize.Width - PictureBox1.Width) \ 2
PictureBox1.Top = (Me.ClientSize.Height - PictureBox1.Height) \ 2
Label1.Text = String.Format("W:{0} H:{1}", PictureBox1.Width, PictureBox1.Height)
Label2.Text = String.Format("W:{0} H:{1}", Me.ClientSize.Width, Me.ClientSize.Height)
End Sub
End Class
-
May 19th, 2026, 03:20 PM
#5
Fanatic Member
Re: Position of a PictureBox
try
Code:
PictureBox1.Left = (Me.ClientSize.Width/2) - (PictureBox1.Width/ 2)
PictureBox1.Top = (Me.ClientSize.Height/2) - (PictureBox1.Height/ 2)
also dont forget that if on a laptop sometimes the scaling is 125% so need to take in to account that
-
May 19th, 2026, 03:33 PM
#6
Re: Position of a PictureBox
 Originally Posted by k_zeon
try
Code:
PictureBox1.Left = (Me.ClientSize.Width/2) - (PictureBox1.Width/ 2)
PictureBox1.Top = (Me.ClientSize.Height/2) - (PictureBox1.Height/ 2)
also dont forget that if on a laptop sometimes the scaling is 125% so need to take in to account that
Did you see Post #4?
-
May 19th, 2026, 03:48 PM
#7
Thread Starter
New Member
Re: Position of a PictureBox
thanks for your answer but i think i found my problem :
me.size is the size of the external dimensions of the form and the (0,0) location for the picturebox is inside the form
-
May 19th, 2026, 09:28 PM
#8
Re: Position of a PictureBox
 Originally Posted by Grenoblus
thanks for your answer but i think i found my problem :
me.size is the size of the external dimensions of the form and the (0,0) location for the picturebox is inside the form
You’re right. Size (or the Bounds rectangle) is not the same as ClientSize (or the ClientRectangle)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|