想要通过从TXT文件中读取坐标来绘制一条线
本文关键字:坐标 读取 绘制 一条 文件 TXT | 更新日期: 2023-09-27 18:16:49
首先我将解释我在做什么:我正在绘制一条线,然后我正在使用StreamWriter在.txt文件中编写该线的两点的坐标!!然后我打开openFiledialog,并读取保存的。txt文件,其中使用streamReader的坐标是安全的,并在同一事件中放置一些逻辑来绘制线,但它不是绘制线,它一直贯穿异常,我试图将字符串转换为使用解析和tryparse的int,但它没有帮助。有人可以引导我从文本文件读取坐标画一条线吗??怎样才能做到呢?我正在使用MOuseDown Event
绘制一条线我正在使用下面的代码
protected void CreateBitmap(int Width, int Height)
{
// Create the background bitmap
BackGroundBitmap = new Bitmap(Width, Height);
// Set the background bitmap to be the Form's background image
BackgroundImage = BackGroundBitmap;
}
string zee1, zee2, zee3, zee4; // Publically declared strings
int m1, n1, m2, n2; // publically declared integers
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
using (StreamReader Reader = new StreamReader(openFileDialog1.FileName, true))
{
zee1 = Reader.ReadLine();
m1 = int.Parse(zee1);
zee2 = Reader.ReadLine();
n1 = int.Parse(zee2);
zee3 = Reader.ReadLine();
m2 = int.Parse(zee3);
zee4 = Reader.ReadLine();
n2 = int.Parse(zee4);
Graphics gra = Graphics.FromImage(BackGroundBitmap);
var Teera = new Pen(Color.Black, 5);
var Nanga1 = new Point(m1, n1);
var Nanga2 = new Point(m2, n2);
gra.DrawLine(Teera, Nanga1, Nanga2);
Teera.Dispose();
}
}
好了,经过多次测试,我想出了这个:
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace ProgramTestDrawLine
{
class Main_Window : Form
{
public Main_Window()
{
this.Width = 500;
this.Height = 500;
this.Paint += Main_Window_Paint;
}
void Main_Window_Paint(object sender, EventArgs e)
{
StreamReader sr = new StreamReader("points.txt");
string fileContents = sr.ReadLine();
sr.Close();
string[] points = fileContents.Split(',');
Point a = new Point(int.Parse(points[0]), int.Parse(points[1]));
Point b = new Point(int.Parse(points[2]), int.Parse(points[3]));
Graphics g = this.CreateGraphics();
g.DrawLine(new Pen(Brushes.Black,2),a, b);
}
}
class Main_W
{
static void Main()
{
Main_Window form = new Main_Window();
Application.EnableVisualStyles();
Application.Run(form);
}
}
}
points.txt文件包含以下单行内容:
15300300
至少对我来说,这是可行的。尝试使用表单。绘制事件,而不是你正在使用的任何其他(我想你正在使用的是窗体。加载事件,这是我最初使用的,它没有绘制线)
Using point &float代替Point &这会有帮助的。下面的代码。
编辑-保存图像,然后将其设置为表单的背景。
protected void CreateBitmap(int Width, int Height)
{
// Create the background bitmap
BackGroundBitmap = new Bitmap(Width, Height);
// Set the background bitmap to be the Form's background image
BackgroundImage = BackGroundBitmap;
}
private void button1_Click(object sender, EventArgs e)
{
CreateBitmap(500, 500);
string zee1, zee2, zee3, zee4; // Publically declared strings
float m1, n1, m2, n2; // publically declared integers
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
using (StreamReader Reader = new StreamReader(openFileDialog1.FileName, true))
{
zee1 = Reader.ReadLine();
m1 = float.Parse(zee1);
zee2 = Reader.ReadLine();
n1 = float.Parse(zee2);
zee3 = Reader.ReadLine();
m2 = float.Parse(zee3);
zee4 = Reader.ReadLine();
n2 = float.Parse(zee4);
Graphics gra = Graphics.FromImage(BackGroundBitmap);
var Teera = new Pen(Color.Black, 5);
var Nanga1 = new PointF(m1, n1);
var Nanga2 = new PointF(m2, n2);
gra.DrawLine(Teera, Nanga1, Nanga2);
BackGroundBitmap.Save("result.jpg");
BackgroundImage = Image.FromFile("result.jpg");
Teera.Dispose();
}
}
}
经过多次实验,我终于找到了答案。如果有人遇到问题,我已经使用了图片框并将其设置为禁用。我也可以用一个面板。但图片盒的目的。首先,我从一个文本文件读取,然后这些坐标被传递给DrawLine,因此最终它绘制线。感谢所有评论和回答的人。真的很有帮助。
下面是代码: private System.Drawing.Graphics g;
private System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Blue, 5);
private void menuItem9_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
using (StreamReader Reader = new StreamReader(openFileDialog1.FileName, true))
{
zee1 = Reader.ReadLine();
textBox1.Text = Convert.ToString(zee1);
u1 = float.Parse(zee1);
zee2 = Reader.ReadLine();
textBox2.Text = zee2;
v1 = float.Parse(zee2);
zee3 = Reader.ReadLine();
textBox3.Text = zee3;
u2 = float.Parse(zee3);
zee4 = Reader.ReadLine();
textBox4.Text = zee4;
v2 = float.Parse(zee4);
Reader.Close();
//BackGroundBitmap.Save("result.jpg"); //test if ok
pictureBox1.Enabled = true;
g = pictureBox1.CreateGraphics();
g.DrawLine(pen1, int.Parse(textBox1.Text), int.Parse(textBox2.Text), int.Parse(textBox3.Text), int.Parse(textBox4.Text));