lorenzo vinco

lorenzo vinco

  • NA
  • 14
  • 3.7k

Drawing a line on a picturebox

May 24 2022 1:08 PM

Hello everyone,

I've started programming on Visul Studio in c# very recently and I have quite little knowledge of programming. 

Currently, what I want to do is to draw a horizontal line on a picturebox after I click on it. The line should pass through the point I click on. 

I was able to write some code that draws the line correctly, but unfortunately the line is drawn on the form and not on the picturebox. I would like someone to explain me how to draw the line upon the picutrebox, with an example code if possible.

Thanks

Code:

        private void Display_pic_MouseUp(object sender, MouseEventArgs e)
        {
            Point MouseLoc = e.Location;
            Point RealMouseLoc = e.Location + new Size(MouseLoc);

            Point start = new Point(0,0);
            start.X = Display_pic.Location.X;
            start.Y = Display_pic.Location.Y;
            mouseUpPositionX = start.X + MouseLoc.X;
            mouseUpPositionY = start.Y + MouseLoc.Y;

            Console.WriteLine(RealMouseLoc);

            Graphics g = this.CreateGraphics();

            g.DrawLine(line, start.X, start.Y + MouseLoc.Y, start.X + Display_pic.Size.Width, start.Y + MouseLoc.Y);

        }

 


Answers (1)