![](/img/trans.png)
[英]Can i retrieve images and shapes from word file and display them in PictureBox control of 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.