Why do colors change after drawing circles?

Why do the circles change colors after drawing ?, in fact, I draw circles, but my problem is that after each double click, the color of the next circles changes from blue to the background color.

public Form1()
    {
        InitializeComponent();
        pictureBox1.Paint += new PaintEventHandler(pic_Paint);
    }

    public Point positionCursor { get; set; }
    private List<Point> points = new List<Point>();
    public int circleNumber { get; set; }

    private void pictureBox1_DoubleClick(object sender, EventArgs e)
    {
        positionCursor = this.PointToClient(new Point(Cursor.Position.X - 25, Cursor.Position.Y - 25));

        points.Add(positionCursor);

        pictureBox1.Invalidate();
    }

    private void pic_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.SmoothingMode = SmoothingMode.AntiAlias;

        foreach (Point pt in points)
        {
            Pen p = new Pen(Color.Tomato, 2);

            g.FillEllipse(Brushes.Blue, positionCursor.X, positionCursor.Y, 20, 20);

            g.DrawEllipse(p, pt.X, pt.Y, 20, 20);

            p.Dispose();
        }
    }

enter image description here

+4
source share
2 answers

You draw ellipses correctly, but you always fill in only one of them (the latter is added to the cursor position).

// This is ok
g.DrawEllipse(p, pt.X, pt.Y, 20, 20);

// You should use pt.X and pt.Y here
g.FillEllipse(Brushes.Blue, positionCursor.X, positionCursor.Y, 20, 20);
+3
source

change pic_Paint as below

 private void pic_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            foreach (Point pt in points)
            {
                Pen p = new Pen(Color.Tomato, 2);
                g.DrawEllipse(p, pt.X, pt.Y, 20, 20);
                g.FillEllipse(Brushes.Blue, pt.X, pt.Y, 20, 20);
                p.Dispose();
            }

        }
0
source

All Articles