gpt4 book ai didi

c# - 如何阻止 GraphicsPath 关闭

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:16 26 4
gpt4 key购买 nike

我正在尝试制作一个可以从 GraphicsPath 中删除点的橡皮擦工具。到目前为止,我的代码允许用户在窗体上绘画,“删除”按钮应该删除 GraphicsPath 的前 20 个点。它一直有效,直到制作出两个可区分的图画,然后按下“删除”按钮 - 如图所示,两个图画连接在一起。我怀疑 GraphicsPath 会自行关闭(连接每个点)。

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

这是我的完整代码。我认为最相关的部分是底部的函数。

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

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

thing

最佳答案

当您从路径中删除点时,您可以通过将它们复制到新路径来执行此操作,但不包括您要删除的点。但是您还没有从第一条路径复制相应的点类型信息;相反,无论出于何种原因,您都将所有点类型重置为 1。这会丢失有关路径中每个图形开始位置的信息。所以发生的是新路径看到一个长连接图形,这解释了你所看到的。
如果你想从路径中删除前 n 个点,你可以尝试这样的事情:

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

关于c# - 如何阻止 GraphicsPath 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47684696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com