简体   繁体   English

从.txt文件获取点并将其绘制在C#的图片框中

[英]Getting points from a .txt file and plotting them in a picturebox in C#

I'm trying to build a windows form application in which I read a comma separated list from a .txt file and then plot that using the DrawLine function. 我正在尝试构建Windows窗体应用程序,在该应用程序中,我从.txt文件中读取逗号分隔的列表,然后使用DrawLine函数进行绘制。 My code keeps getting stuck in an infinite loop and I'm not sure how I should proceed further. 我的代码不断陷入无限循环,我不确定该如何继续。 I have no preference of how I should plot it and the only reason I'm using the drawline function is because I don't know of any other way so I'm open to any other ideas that might be better suited to doing this task. 我对如何绘制它没有偏好,而我使用画线功能的唯一原因是因为我不知道其他任何方式,因此我对可能更适合执行此任务的任何其他想法持开放态度。

       private void startToolStripMenuItem_Click(object sender, EventArgs e)
    {
        StreamReader sr = new StreamReader("../../sample.txt");
        string[] values = null;
        zee1 = sr.ReadLine();
        while (zee1 != null)
        {
            values = zee1.Split(',');
            x1 = Int32.Parse(values[0]);
            x2 = Int32.Parse(values[1]);
            y1 = Int32.Parse(values[2]);
        }



        //BackGroundBitmap.Save("result.jpg"); //test if ok
        Point point1 = new Point(int.Parse(values[0]), int.Parse(values[2]));
        Point point2 = new Point(int.Parse(values[1]), int.Parse(values[2]));

        pictureBox1.Enabled = true;
        g = pictureBox1.CreateGraphics();
        g.DrawLine(pen1, point1, point2);
    }

Please note I'm trying to plot two different values of x on the same plot for the same values of y. 请注意,我正在尝试在同一图上绘制两个不同的x值,以获取相同的y值。 Also that values[0] is an array that contains all the data in the first column of the .txt file and so forth for values[1] and values[2]. 同样,values [0]是一个数组,其中包含.txt文件第一列中的所有数据,对于values [1]和values [2]依此类推。

The txt file I'm using is as follows 我正在使用的txt文件如下

0,4,0 0,4,0

1,2,1 1,2,1

2,1,2 2,1,2

3,6,3 3,6,3

4,1,4 4,1,4

5,3,5 5,3,5

6,8,6 6,8,6

You're in an infinite while loop because the condition never changes. 您处于无限while循环中,因为条件永远不会改变。 You need to update zee1 . 您需要更新zee1 The most popular way to do this is to simply update it in the condition: 最受欢迎的方法是以下情况下对其进行简单更新:

while ((zee1 = sr.ReadLine()) != null)
{
    // what was here already
}

It also seems as if you want to draw a set of line segments (I assume one to the next), so you could do that like so: 似乎您也想绘制一组线段(我假设是一个到下一个),因此可以这样进行:

private void startToolStripMenuItem_Click(object sender, EventArgs e)
{
    List<Point> points = new List<Point>();
    using (StreamReader sr = new StreamReader("../../sample.txt"))
    {
        string[] values = null;
        while ((zee1 = sr.ReadLine()) != null)
        {
            values = zee1.Split(',');
            points.Add(new Point(int.Parse(values[0]), int.Parse(values[2])));
        }
    }

    pictureBox1.Enabled = true;
    g = pictureBox1.CreateGraphics();
    for (int i = 0; i < points.Count - 1; i++)
        g.DrawLine(pen1, points[i], points[i+1]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM