none
Problem using Drawline RRS feed

  • Question

  • I want to test the drawline method but I have difficulties in actually using the method. I have used this code, but the program exits with an exception. Anybody know why??

     

     

     

    private void button3_Click(object sender, EventArgs e)

    {

     

     

    Pen blackPen = new Pen(Color.Black, 3);

     

     

    // Create coordinates of points that define line.

     

     

    int x1 = 100;

     

     

    int y1 = 100;

     

     

    int x2 = 500;

     

     

    int y2 = 100;

     

     

    // Draw line to screen.

    g.DrawLine(blackPen, x1, y1, x2, y2); //g is defined by this code:   graphics g;

    }

    • Moved by Victor_Chen Tuesday, September 14, 2010 9:27 AM drawline (From:Visual Studio Extensibility)
    Tuesday, September 14, 2010 6:06 AM

Answers

  • Hi,

     

    Thorsten gave a great example.

     

    -> but the program exits with an exception.

     

    What is the exception?

     

    If you need to draw on the form, you can try the following:

     

            void button1_Click(object sender, EventArgs e)

            {

                Pen blackPen = new Pen(Color.Black, 3);

     

                // Create coordinates of points that define line.

                int x1 = 100;

                int y1 = 100;

                int x2 = 500;

                int y2 = 100;

     

                // Draw line to screen.

                // g.DrawLine(blackPen, x1, y1, x2, y2); //g is defined by this code:   graphics g;

     

                Graphics g = this.CreateGraphics();

                g.DrawLine(blackPen, x1, y1, x2, y2);

            }

     

    Notice, this drawing is easy to lose when form repainting (minimizing, maximizing and other actions).

     

    It is recommended to draw in the Paint event. The drawing will be not lost when the form repainting.

     

            private void Form1_Load(object sender, EventArgs e)

            {

                button1.Click += new EventHandler(button1_Click);

                this.Paint += new PaintEventHandler(Form1_Paint);

     

                blackPen = new Pen(Color.Black, 3);

            }

     

            int x1;

            int y1;

            int x2;

            int y2;

            Pen blackPen;

            bool NeedToDraw = false;

     

            void Form1_Paint(object sender, PaintEventArgs e)

            {

                if (NeedToDraw)

                    e.Graphics.DrawLine(blackPen, x1, y1, x2, y2);

            }

     

            void button1_Click(object sender, EventArgs e)

            {

                NeedToDraw = true;

     

                // Create coordinates of points that define line.

                x1 = 100;

                y1 = 100;

                x2 = 500;

                y2 = 100;

     

                // force the form to repaint.

                this.Refresh();

            }

     

     

    The following is a drawing FAQ example.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/cfb44920-b9ae-4288-bb42-8586b8dd4358

     

    Hope this helps.

     

    Have a nice day.

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jane Wallace Thursday, September 16, 2010 7:38 AM
    Wednesday, September 15, 2010 9:17 AM
    Moderator

All replies

  • hi,

     

    what kind of exception? any stacktrace?


    BR, Karol. mark as answer/vote as helpful if it helped you
    Tuesday, September 14, 2010 10:55 AM
  • g.DrawLine(blackPen, x1, y1, x2, y2); //g is defined by this code:   graphics g;

    ...

    Hi,


    Graphics g = ?;

    You need to instanciate the variable, e.g. Graphics g = Graphics.FormImage([YourImageVariable]);

    //example; one Form, one PicureBox, Form.Load event hooked to Form1_Load

        private void Form1_Load(object sender, EventArgs e)
        {
          Bitmap bmp = new Bitmap(200, 200);
          Graphics g = Graphics.FromImage(bmp);
          g.FillRectangle(Brushes.Red, new Rectangle(0, 0, bmp.Width, bmp.Height));
          g.Dispose();
    
          this.pictureBox1.Image = bmp;
        }
    

    regards,

      Thorsten

    Tuesday, September 14, 2010 11:17 AM
  • Hi,

     

    Thorsten gave a great example.

     

    -> but the program exits with an exception.

     

    What is the exception?

     

    If you need to draw on the form, you can try the following:

     

            void button1_Click(object sender, EventArgs e)

            {

                Pen blackPen = new Pen(Color.Black, 3);

     

                // Create coordinates of points that define line.

                int x1 = 100;

                int y1 = 100;

                int x2 = 500;

                int y2 = 100;

     

                // Draw line to screen.

                // g.DrawLine(blackPen, x1, y1, x2, y2); //g is defined by this code:   graphics g;

     

                Graphics g = this.CreateGraphics();

                g.DrawLine(blackPen, x1, y1, x2, y2);

            }

     

    Notice, this drawing is easy to lose when form repainting (minimizing, maximizing and other actions).

     

    It is recommended to draw in the Paint event. The drawing will be not lost when the form repainting.

     

            private void Form1_Load(object sender, EventArgs e)

            {

                button1.Click += new EventHandler(button1_Click);

                this.Paint += new PaintEventHandler(Form1_Paint);

     

                blackPen = new Pen(Color.Black, 3);

            }

     

            int x1;

            int y1;

            int x2;

            int y2;

            Pen blackPen;

            bool NeedToDraw = false;

     

            void Form1_Paint(object sender, PaintEventArgs e)

            {

                if (NeedToDraw)

                    e.Graphics.DrawLine(blackPen, x1, y1, x2, y2);

            }

     

            void button1_Click(object sender, EventArgs e)

            {

                NeedToDraw = true;

     

                // Create coordinates of points that define line.

                x1 = 100;

                y1 = 100;

                x2 = 500;

                y2 = 100;

     

                // force the form to repaint.

                this.Refresh();

            }

     

     

    The following is a drawing FAQ example.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/cfb44920-b9ae-4288-bb42-8586b8dd4358

     

    Hope this helps.

     

    Have a nice day.

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jane Wallace Thursday, September 16, 2010 7:38 AM
    Wednesday, September 15, 2010 9:17 AM
    Moderator