首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何阻止GraphicsPath关闭

如何阻止GraphicsPath关闭
EN

Stack Overflow用户
提问于 2017-12-06 22:40:50
回答 1查看 399关注 0票数 2

我试图做一个橡皮擦工具,可以擦除点从一个GraphicsPath。到目前为止,我的代码允许用户在表单上绘制,而“擦除”按钮应该删除GraphicsPath的前20个点。它的工作直到两张可区分的图纸,然后按下“擦除”按钮--正如在图像中看到的,这两幅图连接在一起。我怀疑GraphicsPath关闭了自己(连接每个点)。

是否有办法阻止GraphicsPath连接每个点?

这是我的完整密码。我认为最相关的部分是底部的功能。

代码语言:javascript
复制
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.DoubleBuffered = true;
        }

        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(); // problem is not due to this line


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

        }

        private void btnErase_Click(object sender, EventArgs e)
        {
            List<PointF> ptsList = new List<PointF>();
            for (int i = 0; i < 20; i++)
            {
                ptsList.Add(_drawingPath.PathData.Points[i]);
            }

            _drawingPath = ErasePointsFromPath(_drawingPath, ptsList.ToArray<PointF>());
            this.Invalidate();
        }

        private GraphicsPath ErasePointsFromPath(GraphicsPath path, PointF[] toRemove)
        {
            PointF[] newPoints = path.PathData.Points.Except<PointF>(toRemove).ToArray<PointF>();
            byte[] types = new byte[newPoints.Length];
            for (int i = 0; i < newPoints.Length; i++)
            {
                types[i] = 1;
            }

            GraphicsPath ret = new GraphicsPath(newPoints, types);
            //ret.SetMarkers();
            return ret;
        }
    }
}

事情就是这样的。图中的两条圆线应该是分开的,而不是由对角线连接的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-07 00:00:23

当你擦除路径上的点时,你会将它们复制到一个新的路径中,不包括你想要擦除的点。但是,您也不会从第一条路径复制相应的点类型信息;相反,您将所有点类型重置为1,无论出于什么原因。这将丢失有关路径中每个图形从何处开始的信息。所以所发生的是新的路径看到一个长连接的图形,这解释了你所看到的。

如果您想要从路径中删除前n个点,您可以尝试如下所示:

代码语言:javascript
复制
private void btnErase_Click(object sender, EventArgs e)
{
    int numberOfPointsToErase = 20;

    if (_drawingPath.PointCount > numberOfPointsToErase)
    {
        _drawingPath = new GraphicsPath(
            _drawingPath.PathPoints.Skip(numberOfPointsToErase).ToArray(),
            _drawingPath.PathTypes.Skip(numberOfPointsToErase).ToArray()
        );
    }
    else
    {
        _drawingPath.Reset();
    }

    this.Invalidate();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47684696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档