绘图应用双Graphics上下文下橡皮擦功能异常问题求助
图形编辑程序擦除功能异常问题
代码中有两个Graphics对象:graphics负责创建图形,g用于通过方向键移动图形。原本通过白色画笔实现的擦除功能运行正常,但引入用于移动图形的g对象后,无法在移动后的图形所在上下文上完成擦除(图形创建后会立即绘制到g上下文)。目前临时方案是在MouseMove事件中通过CreateGraphics绘制白色线条,但点击窗体或选择绘图形状时,擦除的痕迹会消失。
以下是核心代码片段:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { startPoint = e.Location; draw = true; if (addtext) { TextBox textBox = new TextBox { Location = pictureBox1.PointToClient(MousePosition), Size = new Size(100, 20), ForeColor = Color.Black }; textBox.Leave += (s, ev) => { TextBox currentTextBox = (TextBox)s; }; pictureBox1.Controls.Add(textBox); textBox.Focus(); addtext = false; } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (draw) { if (choice == 1) { width = e.Location.X - startPoint.X; height = e.Location.Y - startPoint.Y; Rectangle shape = new Rectangle(startPoint.X, startPoint.Y, width, height); graphics.DrawRectangle(pen, shape); graphics.DrawRectangle(eraser, shape); pictureBox1.Refresh(); draw = false; } else if (choice == 2) { width = e.Location.X - startPoint.X; height = e.Location.Y - startPoint.Y; Rectangle shape = new Rectangle(startPoint.X, startPoint.Y, width, height); graphics.DrawEllipse(pen, shape); graphics.DrawEllipse(eraser, shape); pictureBox1.Refresh(); draw = false; } if (draw && choice == 3) { createPolygon(graphics, pen, startPoint, e.Location, 6); createPolygon(graphics, eraser, startPoint, e.Location, 6); draw = false; } else if (choice == 4) { createPolygon(graphics, pen, startPoint, e.Location, 4); createPolygon(graphics, eraser, startPoint, e.Location, 4); draw = false; } else if (choice == 5) { endPoint = e.Location; graphics.DrawLine(pen, startPoint, endPoint); graphics.DrawLine(eraser, startPoint, endPoint); draw = false; } else if (choice == 6) { graphics.DrawRectangle(pen, startPoint.X, startPoint.Y, Math.Abs(e.X - startPoint.X), Math.Abs(e.X - startPoint.X)); graphics.DrawRectangle(eraser, startPoint.X, startPoint.Y, Math.Abs(e.X - startPoint.X), Math.Abs(e.X - startPoint.X)); draw = false; } else if (choice == 7) { graphics.DrawEllipse(pen, startPoint.X, startPoint.Y, e.X - startPoint.X, e.X - startPoint.X); graphics.DrawEllipse(eraser, startPoint.X, startPoint.Y, e.X - startPoint.X, e.X - startPoint.X); draw = false; } else if (choice == 8) { Point[] point = { new Point(startPoint.X, startPoint.Y), new Point(startPoint.X, e.Y), new Point(startPoint.X, e.Y) }; graphics.DrawPolygon(pen, point); Point[] point2 = { new Point(startPoint.X, startPoint.Y), new Point(startPoint.X, endPoint.Y), new Point(endPoint.X, endPoint.Y) }; graphics.DrawPolygon(eraser, point2); draw = false; } else if (choice == 9) { Point[] point = { new Point(startPoint.X, startPoint.Y), new Point((startPoint.X + e.X) / 2, e.Y), new Point(e.X, startPoint.Y) }; graphics.DrawPolygon(pen, point); graphics.DrawPolygon(eraser, point); draw = false; } else if (choice == 10) { graphics.DrawEllipse(pen, startPoint.X, startPoint.Y, e.X - startPoint.X, e.X - startPoint.X); graphics.DrawEllipse(pen, startPoint.X - 20, startPoint.Y - 20, (e.X - startPoint.X) + 40, (e.X - startPoint.X) + 40); graphics.DrawEllipse(eraser, startPoint.X, startPoint.Y, e.X - startPoint.X, e.X - startPoint.X); graphics.DrawEllipse(eraser, startPoint.X - 20, startPoint.Y - 20, (e.X - startPoint.X) + 40, (e.X - startPoint.X) + 40); draw = false;; } Shape newShape = new Shape { Type = choice, StartPoint = startPoint, EndPoint = endPoint, PenColor = pen.Color }; shapes.Add(newShape); pictureBox1.Refresh(); } } private void eraserToolStripMenuItem_Click(object sender, EventArgs e)//This is the box that is used for the erasing part { eraserMode = !eraserMode; draw = true; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (eraserMode) //Here is the code for the Erasor { using (Graphics g = pictureBox1.CreateGraphics()) { g.DrawLine(new Pen(Color.White, 6), startPoint, e.Location); startPoint = e.Location; } } if (draw && choice == 1) { width = e.Location.X - startPoint.X; height = e.Location.Y - startPoint.Y; graphics.DrawRectangle(eraser, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y); graphics.DrawRectangle(pen, startPoint.X, startPoint.Y, width, height); endPoint = e.Location; pictureBox1.Refresh(); } else if (draw && choice == 2) { width = e.Location.X - startPoint.X; height = e.Location.Y - startPoint.Y; graphics.DrawEllipse(eraser, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y); graphics.DrawEllipse(pen, startPoint.X, startPoint.Y, width, height); endPoint = e.Location; pictureBox1.Refresh(); } else if (draw && choice == 3) { createPolygon(graphics, eraser, startPoint, endPoint, 6); createPolygon(graphics, pen, startPoint, e.Location, 6); endPoint = e.Location; pictureBox1.Refresh(); } else if (draw && choice == 4) { createPolygon(graphics, eraser, startPoint, endPoint, 4); createPolygon(graphics, pen, startPoint, e.Location, 4); endPoint = e.Location; pictureBox1.Refresh(); } else if (draw && choice == 5) { graphics.DrawLine(eraser, startPoint, endPoint); graphics.DrawLine(pen, startPoint, e.Location); endPoint = e.Location; pictureBox1.Refresh(); } else if (draw && choice == 6) { graphics.DrawRectangle(eraser, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.X - startPoint.X); graphics.DrawRectangle(pen, startPoint.X, startPoint.Y, e.X - startPoint.X, e.X - startPoint.X); endPoint = e.Location; pictureBox1.Refresh(); } else if (draw && choice == 7) { graphics.DrawEllipse(eraser, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.X - startPoint.X); graphics.DrawEllipse(pen, startPoint.X, startPoint.Y, e.X - startPoint.X, e.X - startPoint.X); endPoint = e.Location; pictureBox1.Refresh(); } else if (draw && choice == 8) { Point[] point2 = { new Point(startPoint.X, startPoint.Y), new Point(startPoint.X, endPoint.Y), new Point(endPoint.X, endPoint.Y) }; Point[] point = { new Point(startPoint.X, startPoint.Y), new Point(startPoint.X, e.Y), new Point(e.X, e.Y) }; graphics.DrawPolygon(eraser, point2); graphics.DrawPolygon(pen, point); endPoint = new Point(e.X, e.Y); // Update endPoint here pictureBox1.Refresh(); } else if (draw && choice == 9) { Point[] point4 = { new Point(startPoint.X, startPoint.Y), new Point((startPoint.X + endPoint.X) / 2, endPoint.Y), new Point(endPoint.X, startPoint.Y) }; Point[] point3 = { new Point(startPoint.X, startPoint.Y), new Point((startPoint.X + e.X) / 2, e.Y), new Point(e.X, startPoint.Y) }; graphics.DrawPolygon(eraser, point4); graphics.DrawPolygon(pen, point3); endPoint = new Point(e.X, e.Y); // Update endPoint here pictureBox1.Refresh(); } else if (draw && choice == 10) { graphics.DrawEllipse(eraser, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.X - startPoint.X); graphics.DrawEllipse(eraser, startPoint.X - 20, startPoint.Y - 20, (endPoint.X - startPoint.X) + 40, (endPoint.X - startPoint.X) + 40); graphics.DrawEllipse(pen, startPoint.X, startPoint.Y, e.X - startPoint.X, e.X - startPoint.X); graphics.DrawEllipse(pen, startPoint.X - 20, startPoint.Y - 20, (e.X - startPoint.X) + 40, (e.X - startPoint.X) + 40); endPoint = e.Location; pictureBox1.Refresh(); } } //and the code that follows is the code that draws the shapes in the g graphics private void pictureBox1_Paint_1(object sender, PaintEventArgs e) { foreach (var shape in shapes) { DrawShape(e.Graphics, shape); } } private void DrawShape(Graphics g, Shape shape) { switch (shape.Type) { case 1: g.DrawRectangle(new Pen(shape.PenColor, 3), new Rectangle(shape.StartPoint, new Size(shape.EndPoint.X - shape.StartPoint.X, shape.EndPoint.Y - shape.StartPoint.Y))); break; case 2: g.DrawEllipse(new Pen(shape.PenColor, 3), new Rectangle(shape.StartPoint, new Size(shape.EndPoint.X - shape.StartPoint.X, shape.EndPoint.Y - shape.StartPoint.Y))); break; case 3: // Add logic for drawing hexagon (6-sided polygon) createPolygon(g, new Pen(shape.PenColor, 3), shape.StartPoint, shape.EndPoint, 6); break; case 4: // Add logic for drawing square (4-sided polygon) createPolygon(g, new Pen(shape.PenColor, 3), shape.StartPoint, shape.EndPoint, 4); break; case 5: g.DrawLine(new Pen(shape.PenColor, 3), shape.StartPoint, shape.EndPoint); break; case 6: g.DrawRectangle(new Pen(shape.PenColor, 3), new Rectangle(shape.StartPoint, new Size(shape.EndPoint.X - shape.StartPoint.X, shape.EndPoint.X - shape.StartPoint.X))); break; case 7: g.DrawEllipse(new Pen(shape.PenColor, 3), new Rectangle(shape.StartPoint, new Size(shape.EndPoint.X - shape.StartPoint.X, shape.EndPoint.X - shape.StartPoint.X))); break; case 8: createPolygon(g, new Pen(shape.PenColor, 3), shape.StartPoint, shape.EndPoint, 3); break; case 9: createPolygon(g, new Pen(shape.PenColor, 3), shape.StartPoint, shape.EndPoint, 3); break; case 10: // Add logic for drawing concentric circles g.DrawEllipse(new Pen(shape.PenColor, 3), new Rectangle(shape.StartPoint, new Size(shape.EndPoint.X - shape.StartPoint.X, shape.EndPoint.X - shape.StartPoint.X))); g.DrawEllipse(new Pen(shape.PenColor, 3), new Rectangle(new Point(shape.StartPoint.X - 20, shape.StartPoint.Y - 20), new Size(shape.EndPoint.X - shape.StartPoint.X + 40, shape.EndPoint.X - shape.StartPoint.X + 40))); break; } }
内容的提问来源于stack exchange,提问作者Andrew Sempros
相关产品推荐
相关产品推荐

