繁体   English   中英

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

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

我正在尝试构建Windows窗体应用程序,在该应用程序中,我从.txt文件中读取逗号分隔的列表,然后使用DrawLine函数进行绘制。 我的代码不断陷入无限循环,我不确定该如何继续。 我对如何绘制它没有偏好,而我使用画线功能的唯一原因是因为我不知道其他任何方式,因此我对可能更适合执行此任务的任何其他想法持开放态度。

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

请注意,我正在尝试在同一图上绘制两个不同的x值,以获取相同的y值。 同样,values [0]是一个数组,其中包含.txt文件第一列中的所有数据,对于values [1]和values [2]依此类推。

我正在使用的txt文件如下

0,4,0

1,2,1

2,1,2

3,6,3

4,1,4

5,3,5

6,8,6

您处于无限while循环中,因为条件永远不会改变。 您需要更新zee1 最受欢迎的方法是以下情况下对其进行简单更新:

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

似乎您也想绘制一组线段(我假设是一个到下一个),因此可以这样进行:

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