how to store coordinates of line in a .txt file ? and load it in software again

  • Thread starter Thread starter PRLED
  • Start date Start date
P

PRLED

Guest
hello

with my software, i can draw lines in a picturebox by mouse (same grid lines)

but i have a problem:

i can draw many lines.... it is ok ,but:

this is very bad that when i open my software i should draw all lines from start!

but i want that (coordinates of all drawn lines) store in a txt file with a button (save).

and when i close the software and re open it again the , i should can load a txt file by browse button (Load).

can you help me

this is my code:

the 122.jpg file (that used in code) is this:

1609432.jpg


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace offside_var
{
public partial class Form1 : Form
{
private Graphics g;
private bool drawingMode = true, lineSelected;
private Pen semiTransPen;
Point[] poly;
public Form1()
{
InitializeComponent();
pictureBox2.Image = Image.FromFile(@"C:\122.jpg");
panel1.Parent = pictureBox2;
panel1.BackColor = Color.Transparent;
var pb = panel1;
pb.Size = new Size(pictureBox2.Image.Width, 400);
pb.Location = new Point(0, 115);
poly = new Point[4];
poly[0] = new Point(pb.Size.Width, 0);
poly[1] = new Point(pb.Size.Width, pb.Size.Height);
Line line = null;
var lines = new List<Line>();

pb.MouseMove += (s, e) =>
{
if (e.Button == MouseButtons.Right && drawingMode)
{
line.End = e.Location;

if (e.Location.Y < 0)
line.End = new Point(e.Location.X, 0);
else if (e.Location.Y > pb.Size.Height)
line.End = new Point(e.Location.X, pb.Size.Height);

if (e.Location.X < 0)
line.End = new Point(0, e.Location.Y);
else if (e.Location.X > pb.Size.Width)
line.End = new Point(pb.Size.Width, e.Location.Y);

pb.Invalidate();
}

};
pb.MouseDown += (s, e) =>
{
if (e.Button == MouseButtons.Right)
{
if (drawingMode)
line = new Line { Start = e.Location, End = e.Location, linePen = Pens.Red };
else
{
lineSelected = false;
foreach (var a in lines)
{
if (a.OnLine(e.Location))
{
a.linePen = semiTransPen = new Pen(Color.FromArgb(75, 0, 0, 255), 5);
lineSelected = true;
if (a.Start.Y > a.End.Y)
{
poly[2] = a.Start;
poly[3] = a.End;
}
else
{
poly[3] = a.Start;
poly[2] = a.End;
}
}
else
a.linePen = semiTransPen = new Pen(Color.FromArgb(1, 0, 0, 255), 5);
}
pb.Invalidate();
}
}
};
pb.MouseUp += (s, e) =>
{
if (e.Button == MouseButtons.Right && drawingMode && line.Magnitude() > 1)
lines.Add(line);
};
pb.Paint += (s, e) =>
{
g = pb.CreateGraphics();
if (lineSelected)
e.Graphics.FillPolygon(new SolidBrush(Color.FromArgb(75, 0, 0, 0)), poly);

if (line != null)
e.Graphics.DrawLine(line.linePen, line.Start, line.End);

foreach (var l in lines)
e.Graphics.DrawLine(l.linePen, l.Start, l.End);

};
this.KeyPress += (s, e) =>
{
if (e.KeyChar == 'e')
{
//MessageBox.Show("sfsdf");
if (drawingMode)
{
foreach (var a in lines)
{
a.linePen = semiTransPen = new Pen(Color.FromArgb(1, 0, 0, 255), 5);
}
}

else
{
foreach (var a in lines)
{
a.linePen = Pens.Red;
}
lineSelected = false;
}
drawingMode = !drawingMode;
pb.Invalidate();
}

if(e.KeyChar == 'q' && !drawingMode)
{
if (poly[0].X == 0)
{
poly[0] = new Point(pb.Size.Width, 0);
poly[1] = new Point(pb.Size.Width, pb.Size.Height);
}
else
{
poly[0] = new Point(0, 0);
poly[1] = new Point(0, pb.Size.Height);
}
pb.Invalidate();
}
};
}

class Line
{
public Pen linePen;
public Point Start { get; set; }
public Point End { get; set; }

public bool OnLine(Point p)
{
if (p == End || p == Start)
return true;

Point v1 = new Point(p.X - Start.X, p.Y - Start.Y);
Point v2 = new Point(p.X - End.X, p.Y - End.Y);

double d1 = v1.X * v2.X + v1.Y * v2.Y;
double d2 = Math.Sqrt(v1.X * v1.X + v1.Y * v1.Y) * Math.Sqrt(v2.X * v2.X + v2.Y * v2.Y);

d1 /= d2;

return (Math.Abs(d1 + 1) < 0.01);
}

public int Magnitude()
{
Point v1 = new Point(End.X - Start.X, End.Y - Start.Y);
return v1.X * v1.X + v1.Y * v1.Y;
}
}
}
}

Continue reading...
 
Back
Top