user1434156
user1434156

Reputation:

Drawing a grid on a PictureBox

I am currently working with a pictureBox. I am having difficulties drawing a grid over the picture box that is approximately the size of 2 x 2 squares. Now the code below is only giving me a line drawn across. How can I proplery draw a full grid on top of the pictureBox?

CODE:

private Graphics g1;

    public Form1()
    {
        InitializeComponent();


        pictureBox1.Image = new Bitmap(500, 500);
        g1 = Graphics.FromImage(this.pictureBox1.Image);
        Pen gridPen = new Pen(Color.Black, 2);
        g1.DrawLine(gridPen, 0, 0, 100, 100);

    }

This what I would like to accomplish: enter image description here

Upvotes: 2

Views: 20438

Answers (2)

Craig.Feied
Craig.Feied

Reputation: 2850

MikeB's accepted answer is, of course, correct, but the OP was still confused and people are still seeing this, so I am adding a clarifying answer (yes, 8 years later).

Mike's method

private void pictureBox1_Paint(object sender, PaintEventArgs e)

is a standalone method that needs to be called at the right time. To make this happen, you need to register it with the OnPaint event dispatcher, so it will be called whenever OnPaint event occurs. To do this you would put the following line into some handy place such as in the constructor of your class:

OnPaint += pictureBox1_Paint;

This approach is OK, but if you get to where you really need drawing performance it will hurt you, because it costs extra for the PaintEventArgs to be constructed and passed to you. A better approach is to override the OnPaint method of the parent class and put your drawing code directly in the OnPaint method. That way you don't have to hook up any event handlers and it runs faster. The code looks almost the same:

protected override void OnPaint (PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            int numOfCells = 200;
            int cellSize = 5;
            Pen p = new Pen(Color.Black);

            for (int y = 0; y < numOfCells; ++y)
            {
                g.DrawLine(p, 0, y * cellSize, numOfCells * cellSize, y * cellSize);
            }

            for (int x = 0; x < numOfCells; ++x)
            {
                g.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCells * cellSize);
            }
        }

One final suggestion: it is almost never a good idea to do drawing in a PictureBox control. A lot of people do it, but not many experts would do it that way. They were not designed to be a drawing canvas -- they just inherit the same functionality that all forms-based controls have -- and they add complexity and slow you down without adding any useful functionality at all. You will be happier if you get rid of the pictureboxes and just draw directly on the form or panel that was holding your picture box.

Upvotes: 3

MikeB
MikeB

Reputation: 2452

I found this question: Efficiently draw a grid in Windows Forms

Here is the gist of it:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            int numOfCells = 200;
            int cellSize = 5;
            Pen p = new Pen(Color.Black);

            for (int y = 0; y < numOfCells; ++y)
            {
                g.DrawLine(p, 0, y * cellSize, numOfCells * cellSize, y * cellSize);
            }

            for (int x = 0; x < numOfCells; ++x)
            {
                g.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCells * cellSize);
            }
        }

Customize accordingly

Upvotes: 8

Related Questions