[英]ArgumentException when using Graphics.DrawLines
I am using visual studio express 2012. 我正在使用Visual Studio Express 2012。
I am trying to draw the NRZI signal. 我正在尝试绘制NRZI信号。 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. System.Drawing.dll中发生了类型为'System.ArgumentException'的未处理异常。其他信息:参数无效。
The error is somewhere in the draws.DrawLines(Pens.Red, NRZI);
错误出现在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);
}
}
}
Looks like you use the wrong Graphics
object. 看起来您使用了错误的Graphics
对象。 In the paint event handler, you need to use the PaintEventArgs.Graphics
property of the supplied PaintEventArgs e
argument: 在paint事件处理程序中,您需要使用提供的PaintEventArgs e
参数的PaintEventArgs.Graphics
属性:
e.Graphics.DrawLines(Pens.Red, NRZI);
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. 如果有人迷路了,我会给出这个答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.