random
random

Reputation: 91

ArgumentException when using Graphics.DrawLines

I am using visual studio express 2012.

I am trying to draw the NRZI signal. But whenever I run my program I always get this error:

An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll Additional information: Parameter is not valid.

The error is somewhere in the draws.DrawLines(Pens.Red, NRZI);

Can somebody tell me why?

Here is my code:

Graphics draws;
Point[] NRZI = new Point[592]; // each binary value equals 74 pixels wide
string data = "10101010";

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{
    int x = 0;

    if (comboBox1.Text == "NRZI") 
    {
        for (int c = 0; c < data.Length; c++)
        {

            if (data.ToCharArray()[c] == '0') // check if binary value is 0
            {
                for (int p = 0; p < 74; p++)
                {
                    NRZI[x] = new Point(x, 109); // point to signify 0 or low
                    x++;
                }
            }
            if (data.ToCharArray()[c] == '1') // check if binary value is 1
            {
                for (int p = 0; p < 74; p++)
                {
                    NRZI[x] = new Point(x, 9);  // point to signify 1 or high
                    x++;
                }                        
            }
        }
        this.Refresh(); // calls paint
        for (w = 0; w < pictureBox1.Width; w++)
        {
            draws.DrawLines(Pens.Red, NRZI);
        }
    }
}

Upvotes: 1

Views: 690

Answers (2)

Bitterblue
Bitterblue

Reputation: 14075

I had the same problem just now. The problem was that the graphics object was disposed. So every property and method of it would throw this exception. I give this answer in case someone else stumbles over it.

Upvotes: 1

George Polevoy
George Polevoy

Reputation: 7681

Looks like you use the wrong Graphics object. In the paint event handler, you need to use the PaintEventArgs.Graphics property of the supplied PaintEventArgs e argument:

e.Graphics.DrawLines(Pens.Red, NRZI);

Upvotes: 2

Related Questions