hi I have been trying to draw a grid to a picture box. I have the grid drawn on the windows form itself but when I try to draw to the picture box it doesn`t work.
instead the grid draws around the area of the picturebox rather than within the picture box itself.
below I have posted my code any help would be greatly apreciated.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

       // int r = 20;
       // int c = 20;
       // const bool[,] grid1 = new bool[r, c];
       // const bool[,] grid2 = new bool[r, c];
        const bool ALIVE = true;
        const bool DEAD = false;
       // int readptr = grid1;
       // int writeptr = grid2;
        int n = 0;

        private void swappointer()
        {
           // bool[,] temp;
           // temp = readpoint;
          //  readpoint = writepoint;
          //  writepoint = temp;
        }

        private void draw()
        {
            //if (writeptr[r, c] == ALIVE)
            {
           //     pen = black;
            }
          //  else
            {
           //     pen = white;
            }

          //  g.DrawRect(pen, xoffset, yoffset);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
           
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
 Pen linePen = new Pen(System.Drawing.Color.CornflowerBlue);
            Graphics grphx = this.CreateGraphics();
            grphx.Clear(this.BackColor);

            for (int i = 1; i < 21; i++)
            {
                //Draw verticle line
                grphx.DrawLine(linePen,
                 (this.ClientSize.Width / 20) * i,
                 0,
                       (this.ClientSize.Width / 20) * i,
                       this.ClientSize.Height);

                //Draw horizontal line
                grphx.DrawLine(linePen,
                 0,
                       (this.ClientSize.Height / 20) * i,
                       this.ClientSize.Width,
                       (this.ClientSize.Height / 20) * i);
            }
            linePen.Dispose();

            //Continues the paint of other elements and controls
            base.OnPaint(e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
        
        }

        private void button2_Click(object sender, EventArgs e)
        {
                
        }
    }
}

Recommended Answers

All 3 Replies

Try something like this:

public partial class Form1 : Form
        {
            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);
            }
        }

... and read it here for some more info: http://www.questionhub.com/StackOverflow/4095260

Thank you very much, this code has solved the problem.

Iam glad it worked.

bye, bye
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.