借鉴PictureBox的折线 [英] Draw polyline on PictureBox

查看:160
本文介绍了借鉴PictureBox的折线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提请折线在图片框(一个或多个线段组成的连续线)。

I want to draw polyline (a continuous line composed of one or more line segments) on PictureBox.

在此,我们可以通过指定各段的端点以及是每一行的距离各段的计算距离创建多行。

In this we can create multiple lines by specifying the endpoints of each segment as well as calculate distance of each segment that is distance of each line.

推荐答案

如果你想这样做的一个图片,最简单的就是继承自己的控制在图片框和提供的功能当你按下鼠标在图片框添加端点。

If you want to do this on a picturebox, the easiest thing is to inherit your own control from a PictureBox and provide the functionality to add endpoints when you mouse down over the picturebox.

然后存储在列表中的鼠标点击的位置,并覆盖的OnPaint 来绘制端点(香港专业教育学院选择了4×4平方)和每个端点之间的线路。这是基本的代码:

You then store the position of the mouse click in a list, and override the OnPaint to draw your endpoints (ive chosen a 4x4 square) and a line between each endpoint. This is the basic code:

public class EndPointPictureBox : PictureBox
{
    private List<PointF> points = new List<PointF>();
    public EndPointPictureBox()
    {
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        points.Add(new PointF(e.X,e.Y));
        base.OnMouseDown(e);
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);

        Graphics g = pe.Graphics;
        foreach(var point in points)
            g.DrawRectangle(Pens.Black,point.X-2.0f,point.Y-2.0f,4.0f,4.0f);
        if(points.Count>1)
            g.DrawLines(Pens.Black,points.ToArray());

    }
}

您现在可以将它添加到一个表就像一个图片框,并选择您imagge里面去它在通常的方式。

You can now add this to a Form just like a PictureBox, and choose your imagge to go inside it in the usual way.

如果您尝试点击该图片框里面几次你会看到它借鉴端点就像你的榜样形象。下面是我的机器的例子:

If you try clicking a few times inside the picturebox you'll see it draws your endpoints just like your example image. Here's an example from my machine:

然后你的下一个要求,让端点之间的距离。这可以通过添加一个类来表示一个端点与它的隔壁邻居的引用来完成。那么它的一些简单的毕达哥拉斯的数学来得到当前点和下之间的距离:

Then your next requirement, get the distance between endpoints. This can be done by adding a class to represent an EndPoint with a reference to its next-door neighbour. Then its some simple Pythagorean mathematics to get the distance between the current point and the next:

public class EndPoint
{
    public EndPoint(int index, List<PointF> points)
    {
        this.Position = points[index];
        if (index < points.Count - 1)
            this.Next = points[index + 1];
    }
    public PointF Position { get; private set; }
    public PointF Next { get; private set; }

    public double GetDistanceToNext()
    {
        if(this.Next == PointF.Empty)
            return 0;

        var xDiff = this.Position.X - Next.X;
        var yDiff = this.Position.Y - Next.Y;

        return Math.Abs(Math.Sqrt((xDiff*xDiff) + (yDiff*yDiff)));
    }
}

和你可以到你的新图片框添加一个方法来获得此列出这些:

And you can add a method to your new PictureBox to obtain this a list of these:

public List<EndPoint> GetEndPoints()
{
    var list = new List<EndPoint>();
    for(var i=0;i<points.Count;i++)
        list.Add(new EndPoint(i,points));
    return list;
}

这篇关于借鉴PictureBox的折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆