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:
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;
}