Wednesday, June 4, 2014

Draw Line Between Two Points with Mouse


namespace Zau_DrawLine_Btn_Two_Point_wMouse
{
    public partial class Form1 : Form
    {
        Bitmap bmap;



        public Form1()
        {
            InitializeComponent();
            bmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        }

        private Point point1, point2;
        List<Point> p1List = new List<Point>();
        List<Point> p2List = new List<Point>();

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (point1.X == 0)
            {
                point1.X = e.X;
                point1.Y = e.Y;
            }
            else
            {
                point2.X = e.X;
                point2.Y = e.Y;

                p1List.Add(point1);
                p2List.Add(point2);

                Invalidate();
                point1.X = 0;
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            using (var p = new Pen(Color.Blue, 2))
            {
                for (int i = 0; i < p1List.Count; i++)
                {
                    e.Graphics.DrawLine(p, p1List[i], p2List[i]);
                }
            }
           pictureBox1.Image = bmap;
        }
    }
}
//Lab Assignment
//Draw a line between the point p1(during mousedown) and point p2 (during mouseup)

No comments:

Post a Comment