簡體   English   中英

使用Graphics.DrawLines時出現ArgumentException

[英]ArgumentException when using Graphics.DrawLines

我正在使用Visual Studio Express 2012。

我正在嘗試繪制NRZI信號。 但是,每當我運行程序時,總會出現此錯誤:

System.Drawing.dll中發生了類型為'System.ArgumentException'的未處理異常。其他信息:參數無效。

錯誤出現在draws.DrawLines(Pens.Red, NRZI);

有人可以告訴我為什么嗎?

這是我的代碼:

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);
        }
    }
}

看起來您使用了錯誤的Graphics對象。 在paint事件處理程序中,您需要使用提供的PaintEventArgs e參數的PaintEventArgs.Graphics屬性:

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

我剛才有同樣的問題。 問題是圖形對象被處置了 因此,它的每個屬性和方法都會引發此異常。 如果有人迷路了,我會給出這個答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM