删除/删除GraphicsPath的部分内容?

时间:2017-12-01 22:28:01

标签: c# .net winforms paint gdi+

当我使用存储在GraphicsPath中的行完成免费绘图时,如何为Paint类型的应用程序制作橡皮擦?

我尝试过以下操作,但看起来Points IEnumerable是只读的。棘手的部分是我必须在绘制的笔划周围留一个薄边框,以便橡皮擦必须保持剩余GraphicsPath周围的边框。

以下是我尝试删除部分GraphicsPath

的内容
    private void Testform_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            List <PointF> list = _drawingPath.PathData.Points.ToList<PointF>();

            for (int i = 0; i < 50; i++)
            {
                list.RemoveAt(i);
            }

            Invalidate();
        }        
    }

这一点仅用于测试,因此没有异常处理和50的任意使用。如果你在表格上涂抹一点,你最终将得到超过50分的测试。

这是我的完整代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;

namespace Cartographer
{
    public partial class testform : Form
    {

        private GraphicsPath _drawingPath = new GraphicsPath();
        private Point lastMouseLocation;
        private bool drawing = false;

        public testform()
        {
            InitializeComponent();
        }

        private void testform_Load(object sender, EventArgs e)
        {
            this.Paint += Testform_Paint;
            this.MouseMove += Testform_MouseMove;
            this.MouseDown += Testform_MouseDown;

            this.DoubleBuffered = true;
        }

        private void Testform_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                List <PointF> list = _drawingPath.PathData.Points.ToList<PointF>();

                for (int i = 0; i < 50; i++)
                {
                    list.RemoveAt(i);
                }

                Invalidate();
            }        
        }

        private void Testform_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                drawing = true;
                _drawingPath.AddLine(lastMouseLocation, e.Location);
                Invalidate();
            }

            if (e.Button == MouseButtons.None && drawing)
            {
                drawing = false;
                _drawingPath.StartFigure();

            }
            lastMouseLocation = e.Location;


        }

        private void Testform_Paint(object sender, PaintEventArgs e)
        {

            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            using (SolidBrush b = new SolidBrush(Color.Blue))
            using (Pen p = new Pen(b, 51))
            {
                p.StartCap = System.Drawing.Drawing2D.LineCap.Round;
                p.EndCap = System.Drawing.Drawing2D.LineCap.Round;
                p.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;

                e.Graphics.DrawPath(p, _drawingPath);

            }

            using (SolidBrush b = new SolidBrush(Color.LightGreen))
            using (Pen p = new Pen(b, 50))
            {
                p.StartCap = System.Drawing.Drawing2D.LineCap.Round;
                p.EndCap = System.Drawing.Drawing2D.LineCap.Round;
                p.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;



                e.Graphics.DrawPath(p, _drawingPath);

            }

        }
    }
}

如果你亲自尝试,你会注意到我需要保持的边界。我需要弄清楚的是如何在保持边界的同时擦除部分图形。

谢谢!

0 个答案:

没有答案