为什么画圈后颜色会变?
Why after drawing circles their colors change?
为什么画圈后颜色变了? ,其实我是画圆圈的,但我的问题是每次双击后,下一个圆圈的颜色从蓝色变为背景色。
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();
}
}
您正确地绘制了椭圆,但您总是只填充其中一个(最后添加的一个,在光标位置)。
// 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);
更改pic_Paint如下
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();
}
}
为什么画圈后颜色变了? ,其实我是画圆圈的,但我的问题是每次双击后,下一个圆圈的颜色从蓝色变为背景色。
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();
}
}
您正确地绘制了椭圆,但您总是只填充其中一个(最后添加的一个,在光标位置)。
// 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);
更改pic_Paint如下
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();
}
}