Click here to Skip to main content
15,743,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone.
How to draw an lines on the PictureBox over the image?

Like this:

Thanks advance.

What I have tried:

using (Graphics g = pictureBox.CreateGraphics())
                Pen p = new Pen(Color.Red, 2.0f);

                for (int n = 1; n <= 50; n++)
                    g.DrawLine(p, n * (Cursor.Position.X), Cursor.Position.Y - 30.0f, n * (Cursor.Position.X), Cursor.Position.Y + 30.0f);


That not working.
Updated 29-Jun-22 9:33am

This might work for you

private void ButtonTest_Click(object sender, EventArgs e)
    using (Graphics g = pictureBox.CreateGraphics())

        float ticksPerSide = 7;   // actually its the number of spaces between ticks
        float tickLength = 20;

        float horzSeperation = pictureBox.Width / ticksPerSide;
        float vertSeperation = pictureBox.Height / ticksPerSide;

        Image image = Image.FromFile("c:\\temp\\image.jpg");

        g.DrawImage(image, new RectangleF(0, 0, pictureBox.Width, pictureBox.Height));

        Pen p = new Pen(Color.Red, 2.0f);

        for (int n = 0; n <= ticksPerSide; n++)
            // Draw the ticks on the top of the picture box
            g.DrawLine(p, n * horzSeperation, 0, n * horzSeperation, tickLength);

            // Draw the ticks on the bottom of the picture box
            g.DrawLine(p, n * horzSeperation, pictureBox.Height, n * horzSeperation, pictureBox.Height - tickLength);

            //Draw the ticks on the left of the picturebox
            g.DrawLine(p, 0, n * vertSeperation, tickLength, n * vertSeperation);

            //Draw the ticks on the right of the picture box
            g.DrawLine(p, pictureBox.Width - tickLength, n * vertSeperation, pictureBox.Width, n * vertSeperation);

        // Draw vertical cross hair line
        g.DrawLine(p, pictureBox.Width / 2, 0, pictureBox.Width / 2, pictureBox.Height);

        // Draw horizontal cross hair line
        g.DrawLine(p, 0, pictureBox.Height / 2, pictureBox.Width, pictureBox.Height / 2);
Share this answer
[no name] 3-Jul-22 8:31am    
Thanks very very much.
"It's not working" is one of the most useless problem descriptions we get: it tells us absolutely nothing about the problem. We don't know if you get an error message, or the wrong data, or even that that code compiles successfully!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So tell us what happens when you run that code, what you expected to happen, how you checked what happened. Help us to help you!

But I'll do what I can...

It depends on what you are trying to do: if you want the lines to be "permanent" than you need to get the Graphics context for the Image that the picturebox contains, draw on that, and then invalidate the PictureBox to force a repaint.

If that isn't what you want, then you probably need to handle the PictureBox.Paint event and draw your lines there.
Share this answer
[no name] 29-Jun-22 5:29am    
Thanks. Sorry for useless description. Really, that "Not functioning" means: I not getting any error but it just not showing the lines what I want. And I want to lines be an forever.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900