locked
Parameter is not valid in Graphics.DrawLine() RRS feed

  • Question

  • Error occurs at the g.DrawLine() of the pictureBox1_MouseMove.

    (Error is "Parameter is not valid")

         public partial class Form1 : Form
        {
            Rectangle _clickRect;
            Point _circleCentre;
            int _circleRadius = 0;
            Bitmap DrawArea;
            Graphics g;
            bool drag;
            Pen blackline_pen;
            Pen redline_pen;
            Pen yellowcircle_pen;
            public Form1()
            {
                InitializeComponent();
                DrawArea = new Bitmap(this.pictureBox1.Size.Width, pictureBox1.Size.Height);
                _clickRect = new Rectangle((pictureBox1.Width / 2) - 10, (pictureBox1.Height / 2) - 10, 20, 20);
                _circleCentre = new Point(_clickRect.X, _clickRect.Y);
                _circleRadius = Convert.ToInt32(_clickRect.Width);
                blackline_pen = new Pen(Color.Black);
                redline_pen = new Pen(Color.Red);
                yellowcircle_pen = new Pen(Color.Yellow, 2f);
                drag = false;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                InitDraw();
            }
            private void InitDraw()
            {
                g = Graphics.FromImage(DrawArea);
                g.DrawLine(blackline_pen, 0, this.pictureBox1.Height / 2, this.pictureBox1.Width, this.pictureBox1.Height / 2);
                g.DrawLine(blackline_pen, this.pictureBox1.Width / 2, 0, this.pictureBox1.Width / 2, this.pictureBox1.Height);
                g.DrawLine(redline_pen, 0, 0, _circleCentre.X, _circleCentre.Y);
                g.DrawLine(redline_pen, _circleCentre.X, _circleCentre.Y, this.pictureBox1.Width, this.pictureBox1.Height);
                g.DrawEllipse(yellowcircle_pen, _circleCentre.X, _circleCentre.Y, 20, 20);
                pictureBox1.Image = DrawArea;
    
                g.Dispose();
            }
    
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {//Get click pos
                var _clickPos = ((MouseEventArgs)e).Location;
    
                var dX = _circleCentre.X + -_clickPos.X;
                var dY = _circleCentre.Y - _clickPos.Y;
    
                //Check if click is inside rectangle
                if (_circleRadius >= Math.Sqrt((dX * dX) + (dY * dY)))
                {
                    drag = true;
                }
                else
                {
                    drag = false;
                }
            }
    
            private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                if (drag==true)
                {
                    if (e.X <= pictureBox1.Width)
                    {
                        _circleCentre.X = e.X;
                    }
                    if (e.Y <= pictureBox1.Height)
                    {
                        _circleCentre.Y = e.Y;
                    }
                    g.DrawLine(yellowcircle_pen, 0, 0, _circleCentre.X, _circleCentre.Y);
                }
            }
    
            private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                drag = false;
            }
        }
    Any help?


    • Edited by Jeff0803 Wednesday, March 30, 2016 5:11 PM
    Wednesday, March 30, 2016 5:09 PM

Answers

  • Try replacing DrawLine in MouseMove with these two lines:

       DrawArea = new Bitmap( this.pictureBox1.Size.Width, pictureBox1.Size.Height );

       InitDraw();

    But it can be probably further fixed and optimised.

    • Proposed as answer by Kristin Xie Thursday, March 31, 2016 7:17 AM
    • Marked as answer by Kristin Xie Thursday, April 7, 2016 1:50 AM
    Wednesday, March 30, 2016 6:35 PM

All replies

  • Hi,

    if you want to use a global GraphicsObject ("g" in your case), dont dispose it before the end of its use (eg the Form closing event)

    Remove:

    g.Dispose(); from the InitDraw method

    and add a pictureBox1.Refresh() after the drawLine statement.

    But you'll see very quickly whats wrong with your approach ... :-)

    Regards,

      Thorsten



    Wednesday, March 30, 2016 6:32 PM
  • Try replacing DrawLine in MouseMove with these two lines:

       DrawArea = new Bitmap( this.pictureBox1.Size.Width, pictureBox1.Size.Height );

       InitDraw();

    But it can be probably further fixed and optimised.

    • Proposed as answer by Kristin Xie Thursday, March 31, 2016 7:17 AM
    • Marked as answer by Kristin Xie Thursday, April 7, 2016 1:50 AM
    Wednesday, March 30, 2016 6:35 PM
  • Thanks it works fine

    Wednesday, March 30, 2016 7:00 PM