How to draw lines in C#? (about offside lines of football )

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

PRLED

Guest
hello

I want to display the offside virtual line on the football field in my C# software. I will draw many lines inside a picturebox (this is simple) but I want all lines be hidden when run program and when I do right-click mouse anywhere inside the picturebox program should automatically show only one line that I clicked on mouse location in picturebox.

I wrote this code but I know it's wrong and just paint like pen in the picturebox

1603922.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 WindowsFormsApplication4
{
public partial class Form1 : Form
{
class Line
{
public Point Start { get; set; }
public Point End { get; set; }
}

public Form1()
{
InitializeComponent();
pictureBox1.Controls.Add(pictureBox2);

pictureBox2.BackColor = Color.Transparent;
pictureBox1.Image = Image.FromFile(@"C:/1111.jpg");

var pb = pictureBox2;
Line line = null;

pb.MouseMove += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
line.End = e.Location;
pb.Invalidate();
}
};
pb.MouseDown += (s, e) =>
{
if (e.Button == MouseButtons.Left)
line = new Line { Start = e.Location, End = e.Location };
};
var lines = new List<Line>();
pb.MouseUp += (s, e) =>
{
if (e.Button == MouseButtons.Left)
lines.Add(line);
};
pb.Paint += (s, e) =>
{
if (line != null)
e.Graphics.DrawLine(Pens.Red, line.Start, line.End);

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

};

}

private void button1_Click(object sender, EventArgs e)
{

pictureBox2.BackColor = Color.Transparent;
}
}
}

Continue reading...
 
Back
Top