Results 1 to 8 of 8

Thread: Position of a PictureBox

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2026
    Posts
    3

    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

  2. #2

    Thread Starter
    New Member
    Join Date
    May 2026
    Posts
    3

    Re: Position of a PictureBox

    Attachment 196098

    Voici l'image

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Fanatic Member
    Join Date
    Nov 2011
    Posts
    806

    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

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Position of a PictureBox

    Quote Originally Posted by k_zeon View Post
    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?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2026
    Posts
    3

    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

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,421

    Re: Position of a PictureBox

    Quote Originally Posted by Grenoblus View Post
    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width