[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
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.
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
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.
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
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.