0

What should I do in the AddPoint method and what to do in the pictureBox2 paint event? I want to add the point on the mouse cursor position.

private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;

            x = 0;
            y = 0;

            if (pictureBox2.Image != null && selectedPath != null)
            {
                if ((x >= 0 && x <= pictureBox2.Image.Size.Width) && (y >= 0 && y <= pictureBox2.Image.Size.Height))
                {
                    DrawingRects.Add(new DrawingRectangle()
                    {
                        Location = e.Location,
                        Size = Size.Empty,
                        StartPosition = e.Location,
                        Owner = (Control)sender,
                        DrawingcColor = SelectedColor
                    });
                }
            }
        }

This is a method where i'm drawing the filled ellipses in each drawn rectangle 4 corners. the problem is that the top left drawn point is inside the rectangle corner the other 3 points are outside the rectangle corners.

how can i make either all points to be inside the rectangle corners or all the points to be on the drawn rectangle line corners?

private void DrawShapes(Graphics g)
        {
            if (DrawingRects.Count == 0) return;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            foreach (var dr in DrawingRects)
            {
                if (dr.Rect.Width > 0 && dr.Rect.Height > 0)
                {
                    using (Pen pen = new Pen(dr.DrawingcColor, DrawingRectangle.PenSize))
                    {
                        g.DrawRectangle(pen, dr.Rect);
                        g.FillEllipse(Brushes.Red, new Rectangle(dr.Rect.Left, dr.Rect.Top,15,15));
                        g.FillEllipse(Brushes.Red, new Rectangle(dr.Rect.Left, dr.Rect.Bottom, 15, 15));
                        g.FillEllipse(Brushes.Red, new Rectangle(dr.Rect.Right, dr.Rect.Top, 15, 15));
                        g.FillEllipse(Brushes.Red, new Rectangle(dr.Rect.Right, dr.Rect.Bottom, 15, 15));
                    };
                }
            }
        }

and calling it in the paint event:

private void pictureBox2_Paint(object sender, PaintEventArgs e)
    {
        if (pictureBox2.Image != null && selectedPath != null && DrawingRects.Count > 0)
        {
            DrawShapes(e.Graphics);
        }
    }

The result now:

rectangle

The DrawingRectangle class:

public class DrawingRectangle
    {
        public Rectangle Rect => new Rectangle(Location, Size);
        public Size Size { get; set; }
        public Point Location { get; set; }
        public Control Owner { get; set; }
        public Point StartPosition { get; set; }
        public Color DrawingcColor { get; set; } = Color.LightGreen;
        public static float PenSize { get; set; } = 3f;
    }
jhon barns
  • 23
  • 6
  • 1
    `x` and `y` are always zero in your post. Sounds like you need a `private List points` variable to add the points to, and then in the Paint event, enumerate over that collection to draw your circles. It's not clear if you have `DrawingRects` why you would have `AddPoint`. – LarsTech Feb 24 '23 at 22:37
  • @LarsTech right i also doing a loop over the DrawingRects in the paint already. how do i find a rectangle 4 corners positions to add a fillellipse to each corner of the rectangle ? – jhon barns Feb 24 '23 at 23:14
  • Your `DrawingRects` class looks a little weird to me with Location, StartPosition being the same value, and Size being empty. A solid circle is just Top-Left corner, Width and Height. If the mouse down position is meant to be the center, then you need to divide the width and height by two and subtract that amount from the x, y position to get the Top-Left corner. – LarsTech Feb 24 '23 at 23:22
  • DrawingRects is: List DrawingRects = new List(); and the class DrawingRectangle is in the question I edited with this class at the bottom. – jhon barns Feb 24 '23 at 23:27
  • I see now what you mean by four corners of the rectangle. Try `Left - 8, Top - 8, 16, 16` for each of those circles. – LarsTech Feb 24 '23 at 23:30
  • - 8 did the trick. thanks. – jhon barns Feb 24 '23 at 23:50
  • [How can I treat the circle as a control after drawing it? - Moving and selecting shapes](https://stackoverflow.com/q/38345828/3110834) – Reza Aghaei Feb 26 '23 at 16:40

0 Answers0