Results 1 to 6 of 6

Thread: [RESOLVED] Not getting proper X1 and Y1 co-ordinates of Square parallel to Square PictureBox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2024
    Posts
    133

    Resolved [RESOLVED] Not getting proper X1 and Y1 co-ordinates of Square parallel to Square PictureBox

    Hello

    If you observe the image the gap is more on Right and Bottom Side of Square Drawn in Square of Picturebox

    First of all would like to know the Location of Image drawn in picture box.
    Is the Img position from Left of PictureBox or position from Left of the Form ?
    As per the below trial It seems location of image starts from Left of Form and not the Picture box
    Code:
    PictureBox1 Design Form Properties
    PictureBox1.Location = 288, 3
    PictureBox1.Size = 720, 720
    Code:
    Public Class Square
    
    Private SqImg As New Bitmap(700, 700)
    
    Private Sub Square_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
      DrawSquare(gr, New Point(0, 0), 700, New Pen(Brushes.Blue))
      PictureBox1.Image = SqImg
    
    End Sub
    
    Public Sub DrawSquare(ByVal g As Graphics, ByVal center As Point, ByVal SqEdgeSize As Integer, ByVal pen As Pen)
      
            Dim sqEdges As Integer = 700 
    
            Dim pointXPicbox As Integer = PictureBox1.Location.X   '288
            Dim pointYPicbox As Integer = PictureBox1.Location.Y   '3
    
            Dim sizeXPbox As Integer = PictureBox1.Width '720
            Dim SizeYPbox As Integer = PictureBox1.Height 
    
            Dim diffofXpoint As Integer = (PictureBox1.Width - SqEdgeSize)
            Dim diffofYpoint As Integer = (PictureBox1.Height - SqEdgeSize)
    
            Dim RectNewXLoc As Integer = diffofXpoint / 2
            Dim RectNewYLoc As Integer = diffofYpoint / 2
    
            Dim RectNewX1Loc As Integer = PictureBox1.Width - RectNewXLoc - 21 'RectNewXLoc
            Dim RectNewY1Loc As Integer = PictureBox1.Height - RectNewYLoc - 21 '20
    
            Dim square As Rectangle = New Rectangle(RectNewXLoc, RectNewYLoc, RectNewX1Loc, RectNewY1Loc)
    
            g.DrawRectangle(pen, square)
    
    End Sub
    Any appropriate calculation or Formula to draw the square of any size LESS THAN Square Picturebox Size to derive the X1 and Y1 positions which are respectively mentioned as RectNewX1Loc, RectNewY1Loc in above code. if you see value 21 had to be manually incorporated. Would liked to avoid entering the values of difference of Square PictureBox Size to Img Square Size.

    Thanks
    nkvb
    Attached Images Attached Images  
    Last edited by nkvb; Jan 19th, 2025 at 09:57 AM.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 2024
    Posts
    133

    Re: Not getting proper X1 and Y1 co-ordinates of Square parallel to Square PictureBox

    FordPrefect

    Thank you so much. Indeed this was very useful

    Except there were two errors which has been modified and corrected in Blue
    Code:
    ' Form1 with PictureBox1 and Button1
    Public Class Form1
    	Dim r As New Random
    	Private SqImg As New Bitmap(700, 700)
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With PictureBox1
    			.Location = New Point(288, 3)
    			.Size = SqImg.Size
    		End With
    
    		DrawSquare(SqImg, New Size(400, 400), New Pen(Brushes.Blue, 12))
    	End Sub
    
    	Public Sub DrawSquare(im As Bitmap, sz As Size, pen As Pen)
    		Dim g As Graphics = Graphics.FromImage(im)
    		'Dim loc As New PointF((im.Width - sz.Width) \ 2, (im.Height - sz.Height) \ 2)
    		Dim loc As New Point((im.Width - sz.Width) \ 2, (im.Height - sz.Height) \ 2)
    
                    Using p As New Pen(pen.Color, pen.Width)
    			'g.DrawRectangle(pen, New RectangleF(loc, sz))
                      	g.DrawRectangle(pen, New Rectangle(loc, sz))
                    End Using
             End Sub
    
    	Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    		e.Graphics.DrawImage(SqImg, 0, 0)
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		Dim x As Integer = r.Next(0, SqImg.Width)
    		Dim c As Color = Color.FromArgb(r.Next(100, 256), r.Next(0, 256), r.Next(0, 256))
    		DrawSquare(SqImg, New Size(x, x), New Pen(c, 12))
    		PictureBox1.Invalidate()
    	End Sub
    End Class
    Thanks nkvb
    Last edited by nkvb; Jan 19th, 2025 at 11:37 PM.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,949

    Re: Not getting proper X1 and Y1 co-ordinates of Square parallel to Square PictureBox

    PointF and RectangleF take coordinates of Type Single, as opposed to Point and Rectangle which take coordinates of Type Integer. As screen coordinates are all Integers, it makes sense not using the Single options, but beware of rounding errors.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2024
    Posts
    133

    Re: Not getting proper X1 and Y1 co-ordinates of Square parallel to Square PictureBox

    .Paul. Sir,
    Thank you for pointing it out.
    PointF and RectangleF take coordinates of Type Single, as opposed to Point and Rectangle which take coordinates of Type Integer. As screen coordinates are all Integers, it makes sense not using the Single options, but beware of rounding errors.
    After your above comment I retained the original code posted by FP and failed in handling the error
    Will really appreciate if one can correct it retaining the original code
    Code:
    .......
    	Dim loc As New PointF((im.Width - sz.Width) \ 2, (im.Height - sz.Height) \ 2)
           .......          
                g.DrawRectangle(pen, New RectangleF(loc, sz))
        
    ERROR: BC30311 Value of RectangleF cannot be converted to Rectangle 
    Thanks
    nkvb

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,949

    Re: Not getting proper X1 and Y1 co-ordinates of Square parallel to Square PictureBox

    Here's the documentation for the Graphics.DrawRectangle Method

    https://learn.microsoft.com/en-us/do...owsdesktop-9.0

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2024
    Posts
    133

    Re: Not getting proper X1 and Y1 co-ordinates of Square parallel to Square PictureBox

    FordPrefect
    Hi

    You also failed to tell us what the 'two errors' were!

    However, both versions work fine here, the code as posted above and the code below. I assume we are on different versions of VB, mine is:
    VS Community 2022 (64bit) Preview
    Version 17.13.0 Preview 2.1
    .NET v4.8 09032
    Thank you so much for the clarification. FYI Whenever I post the thread I choose the prefix as VS2019

    You also failed to tell us what the 'two errors' were!
    Later on I realized when .Paul. Sir posted his comments in #5. It seemed that I had to mark the Error Nos.

    Thanks Once More

    nkvb
    Last edited by nkvb; Jan 20th, 2025 at 10:56 AM.

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